Diamond Mining Profits
InterviewTime limit1sMemory limit128 MB
Find the contiguous period with the largest total gain across up to 2000 test cases, breaking ties by shorter length then earlier start.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array, Prefix sum
- Solved
- No attempts yet
Problem
You run a business that mines diamonds from a river. The government lets you lower the machine into the river once, and you have to pull it out before the permit runs out. After you lower it, you may pull it out on any day you choose.
The machine uses a diamond as its drilling head, so running it costs diamonds. Your company owns a precise detector that tells you in advance, for every day from the first day of the permit to the last one, how many diamonds you gain or lose that day.
Write a program that finds the period in which your company earns the most diamonds.
Input
The first line contains the number of test cases . ()
Each of the next lines contains one test case. A test case consists of numbers. The first number is the total number of days the government grants, . () The next numbers are the predicted gain or loss of diamonds on each day, and every one of them is an integer between and .
Output
Print lines, one per test case. On each line print the starting day, the ending day, and the largest total profit, in that order. The first day of the input is day 1 and the last one is day .
If no period gives a positive profit, print 0 0 0. If several periods give the same profit, choose the shorter one, and if their lengths are equal as well, choose the one that starts earlier.