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:30:13
Task:Buy Low, Sell High
Sender:KnowYourArchitecture
Submission time:2018-05-26 11:30:13
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;


int main(){
	int n;
	cin>>n;
	vector<ll> p(n);
	for (ll &a : p) cin >> a;
	vector<ll> f(n);
	ll m = p[0];
	for (int i = 0; i < n; i++) {
		m = min(m, p[i]);
		f[i] = p[i] - m;
	}
	vector<ll> s(n);
	ll M = p[n-1];
	for (int i = n-1; i>=0; i--) {
		M = max(M, p[i]);
		s[i] = M - p[i];
	}
	ll B = 0;
	ll b = -1;
	for (int i = 0; i < n; i++) {
		B = max(B, f[i]);
		ll t = B + s[i];
		if (t > b) b = t;
	}
	for (int i=0;i<n;i++){
	//cout<<f[i]<<" "<<s[i]<<endl;
	}
	cout<<b<<endl;
}