Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

翻译:
给定一个数列{N1,N2,...,Nk},连续子序列定义为{Ni,Ni + 1,...,Nj},其中1 <= i <= j <= K,最大子序列是其元素总和最大的连续子序列。
例如,给定序列{-2,11,-4,13,-5,-2},其最大子序列为{11,-4,13},最大和为20。
你需要算出最大的和,连同子序列的第一个和最后一个数字。
输入:
每一个输入文件包含一个测试用例,每一个测试用例包含2行,第一行包括一个正整数K(K<=10000),第二行包括K个数字,用空格分隔。
输出:
对于每一个测试用例,在一行内输出最大子序列的和,以及子序列的第一个数字和最后一个数字的值,之间用空格分隔,但是结尾必须
没有多余的空格,在最大子序列不唯一的情况下,输出具有最小索引i和j(如示例情况所示)的索引。如果所有K个数字都为负数,
则其最大和定义为0,并且您应该输出整个序列的第一个和最后一个数字。
思路:
sum为所求最大和,leftindex为最大子序列的第一个数字的下标,rightindex为最大子序列的最后一个数字的下标,temp代表当前
临时的序列和,tempindex为但当前序列的第一个数字下标。
当temp小于0,temp重置为0,并将当前left设为i,重新进行新的子序列的计算,如果temp大于sum,更新sum,leftindex,rightindex的值 


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
int main() {
	int n;
	scanf("%d", &n);
	vector v(n);
	int leftindex = 0, rightindex = n - 1, sum = -1, temp = 0, tempindex = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &v[i]);
		temp = temp + v[i];
		if (temp < 0) 
		{ 
				temp = 0; 
			tempindex = i + 1; 
		} 
		else if (temp > sum) 
		{ 
			sum = temp;
			leftindex = tempindex;
			rightindex = i;
		}
	}
	if (sum < 0) sum = 0;
	printf("%d %d %d", sum, v[leftindex], v[rightindex]);
	return 0;
}