中国大学MOOC-陳越、何欽銘-データ構造-2018秋Case 1 01-複雑度2 Maximm Subsequence Sum(25分)

712 ワード

#include 

#define N 10000

using namespace std;

int main()
{
	
	int i, n, a[N], left = 0, right = 0, maxsum = -1, 
		currentsum = 0, temp = 0, flag = 0;
		
	cin >> n;
	for (i = 0; i < n; i++) {
		
			cin >> a[i];
			
	}
	
	for (i = 0; i < n; i++){
		
		currentsum += a[i];
		
		if (currentsum > maxsum) {
			
			maxsum = currentsum;
			left = temp;
			right = i;
			flag = 1;
			
		} 
		
		if (currentsum < 0) {
			
			currentsum = 0;
			temp = i + 1;
						
		}
	}
	
	if (0 == flag) cout << 0 << " " << a[0] << " " << a[n - 1] << endl;
	else cout << maxsum << " " <