Maximal Sum
InterviewTime limit1sMemory limit64 MB
Find the contiguous block of edge weights with the largest sum and print its endpoints, or print no good path when the best sum is not positive.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
A graph has nodes in a line, numbered 1 to . The edge joining node and node has weight , and a weight can be positive or negative.
For , the value of the path from node to node is the sum of the weights the path crosses, .
Write a program that finds the path with the largest value.
Input
The first line contains the number of test cases ().
Each test case starts with the number of nodes (), followed by the integers . Here is the weight of the edge joining node and node , and . Numbers are separated by whitespace or line breaks.
Output
For each test case, print on one line the starting node and the ending node of the path with the largest value, separated by a space.
If several paths reach the largest value, choose the longest one, the one whose is largest. If several paths share both the largest value and the same length, choose the one with the smallest .
If the largest value is not positive, print no good path.