Task: | Buy Low, Sell High |
Sender: | Tefyn virallinen maajoukkue |
Submission time: | 2018-05-26 12:00:53 +0300 |
Language: | C++ |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.12 s | details |
#2 | ACCEPTED | 0.21 s | details |
#3 | ACCEPTED | 0.19 s | details |
#4 | ACCEPTED | 0.26 s | details |
#5 | ACCEPTED | 0.27 s | details |
#6 | ACCEPTED | 0.01 s | details |
#7 | ACCEPTED | 0.01 s | details |
#8 | ACCEPTED | 0.01 s | details |
#9 | ACCEPTED | 0.12 s | details |
Compiler report
input/code.cpp: In function 'int main()': input/code.cpp:17:13: warning: unused variable 'ans2' [-Wunused-variable] int64_t ans2 = 0; ^~~~
Code
#include <bits/stdc++.h> using namespace std; int64_t mi[505050], im[505050], ma[505050], am[505050], a1[505050], a2[505050]; int main(){ int n; cin >> n; vector<int64_t> p; for(int i = 0; i < n; ++i){ int64_t tmp; cin >> tmp; p.push_back(tmp); } ma[0] = p[0]; mi[0] = p[0]; am[n-1] = p[n-1]; im[n-1] = p[n-1]; int64_t ans1 = 0; int64_t ans2 = 0; for(int i = 1; i < n; ++i){ ma[i] = max(ma[i-1], p[i]); mi[i] = min(mi[i-1], p[i]); } for(int i = n-2; i >= 0; --i){ am[i] = max(am[i+1], p[i]); im[i] = min(im[i+1], p[i]); } //cout << "a1 "; for(int i = 0; i < n; ++i){ a1[i] = p[i]-mi[i]; if(i > 0){ a1[i] = max(a1[i], a1[i-1]+ma[i]-ma[i-1]); } // cout << a1[i] << " "; } //cout << endl; //cout << "a2 "; for(int i = n-1; i > 0; --i){ a2[i] = am[i]-p[i]; if(i < n-2){ a2[i] = max(a2[i], a2[i+1]+im[i+1]-im[i]); } //cout << a2[i] << " "; } // cout << endl; ans1 = max(a1[n-1], a2[0]); for(int i = 0; i < n-1; ++i){ ans1 = max(ans1, a1[i]+a2[i+1]); } cout << ans1 << endl; return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
500000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 2
Verdict: ACCEPTED
input |
---|
500000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
499999 |
user output |
---|
499999 |
Test 3
Verdict: ACCEPTED
input |
---|
500000 500000 499999 499998 499997 49... |
correct output |
---|
0 |
user output |
---|
0 |
Test 4
Verdict: ACCEPTED
input |
---|
500000 617752857 533265574 365848360 ... |
correct output |
---|
1999980408 |
user output |
---|
1999980408 |
Test 5
Verdict: ACCEPTED
input |
---|
500000 209620375 316066031 756114325 ... |
correct output |
---|
1999992655 |
user output |
---|
1999992655 |
Test 6
Verdict: ACCEPTED
input |
---|
1 1 |
correct output |
---|
0 |
user output |
---|
0 |
Test 7
Verdict: ACCEPTED
input |
---|
2 1 1 |
correct output |
---|
0 |
user output |
---|
0 |
Test 8
Verdict: ACCEPTED
input |
---|
2 2 1 |
correct output |
---|
0 |
user output |
---|
0 |
Test 9
Verdict: ACCEPTED
input |
---|
500000 1 1000000000 2 2 2 2 2 2 2 2 2... |
correct output |
---|
1999999998 |
user output |
---|
1999999998 |