Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 11:57:17
2018-05-26 11:45:44
Task:Buy Low, Sell High
Sender:barely div 2.8 burgeria
Submission time:2018-05-26 11:57:17
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <string>

using namespace std;

typedef long long LL;

int main(){
	int n; cin>>n;
	vector<LL> nums;
	vector<LL> cumleft(n);
	vector<LL> cumright(n);
	LL tot = 0;
	for (int i = 0; i < n; i++) {
		LL qwe; cin>>qwe;
		tot += qwe;
		cumleft[i] = tot;
		nums.emplace_back(qwe);
	}
	tot = 0;
	for (int i = n-1; i >= 0; i--) {
		tot += nums[i];
		cumright[i] = tot;
	}
	vector<LL> left(n);
	vector<LL> right(n);
	
	LL minv = (LL)1 << 62;
	LL maxv = -1;
	for (int i = 0; i < n; i++) {
		//minv = min(cumleft[i], minv);
		minv = min(nums[i], minv);
		left[i] = nums[i] - minv;
		if (i > 0) left[i] = max(left[i], left[i-1]);
	}

	minv = (LL)1 << 62;
	maxv = -1;
	for (int i = n-1; i >= 0; i--) {
		maxv = max(nums[i], maxv);
		right[i] = maxv - nums[i];
		if (i < n-1) right[i] = max(right[i], right[i+1]);
	}
	LL ans = 0;
	for (int i = 0; i < n; i++) {
//cout << left[i] << "x" << right[i] <<endl;
		ans = max(ans, left[i] + right[i]);
	}
	cout << ans;
}