#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100000 int main(){ int t;…
- User
#include <stdio.h> #include <stdlib.h>
#define MAX_SIZE 100000
int main(){ int t; scanf("%d",&t); while(t--){ int a; scanf("%d",&a); int c[MAX_SIZE]; for(int i=0;i<a;++i){ scanf("%d",&c[i]); } int stk[MAX_SIZE], in_stack[MAX_SIZE]; int e = -1, f = -1; long long jk = 0; for (int i=0;i<a;++i){ while(e>0 && stk[e]>c[i]){ int u = stk[e--]; if(u >= stk[e]){ f--; } else{ stk[++e] = u; break; } } stk[++e] = c[i]; in_stack[++f] = i; }
int b = stk[e--]; int g = in_stack[f--]; while(e >= 0){ jk += (long long)(b > stk[e] ? b : stk[e]) * (g - in_stack[f]); b = stk[e--]; g = in_stack[f--]; } printf("%lld\n",jk); } return 0;}
Wrong Answer - failed on a hidden test case (0.02)
Graph Cost You have an array A of size N, and a complete weighted graph with N nodes and
(N⋅(N−1))/2 undirected weighted edges connecting each pair of nodes.
The weight of the edge between node i and node j is ∣j−i∣⋅max(A i,A j). The distance between 2 nodes is defined as the minimum sum of weights on a path connecting the 2 nodes.
Find the distance between nodes 1 and N.
Note that ∣x∣ denotes the absolute value of x. For example, ∣−7∣=7 and ∣4∣=4.
Input Format The first line of input will contain a single integer T, denoting the number of test cases. Each test case consists of multiple lines of input. The first line of each test case contains a single integer N, the number of nodes in the graph and the size of array A. The second line of each test case contains N integers, A 1,A 2 ,…,A N. Output Format For each test case, output on a new line, the distance between nodes 1 and N.
Constraints 1≤T≤2⋅10^4 1≤N≤2⋅10 ^5
1≤A i≤N The sum of N over all test cases does not exceed 2⋅10^5.
Sample 1: Input: 3 4 2 1 1 1 3 3 2 1 5 2 4 5 2 3
Output: 4 5 9
Explanation: Test case 1: The optimal path is 1→2 and then 2→4 with sum of weights 2+2=4.
Test case 2: The optimal path is 1→2 and then 2→3 with sum of weights 3+2=5.
write C++ program
- Assistant