Graph Cost You have an array A of size N, and a complete weighted graph with N nodes and…
- User
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
- User
Time Limit Exceeded (1.02) - time
- Assistant
- User
#include #include #include #include
- Assistant