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

Show test data

Code

#include <bits/stdc++.h>

#define ll long long
#define lll __int128
#define pii pair<int, int>
#define M 1000000007
#define N (1<<20)
using namespace std;

int t[N], z[N];

int main () {
	ll nn;
	cin>>nn;
	int c = 1e9;
	for(int i=1; i<=nn; ++i){
		int a; cin >> a;
		z[i] = a;
		t[i] = max(t[i-1], a-c);
		c = min(a, c);
	}
	int V = 0;
	c=0;
	for(int i=nn; i>=1; --i){
		c = max(c, z[i]);
		V = max(V, t[i]+c-z[i]);
	}
	cout << V;
}