#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
ll n;
cin>>n;
ll prev = 0;
ll tot = 0;
stack<pair<ll,ll>> s; //index, time
for (ll i = 0; i<n; i++){
ll val;
cin>>val;
if (val<=prev){prev=val;continue;}
prev=val;
ll ind = i;
tot+=(n-ind)*val;
while (s.size()>0){
auto f = s.top();
ll tind = f.first;
ll th = f.second;
//cout<<tind<<' '<<th<<'\n';
//can we do better by popping?
if ((n-ind)*val+(n-tind)*th>=(n-tind)*val){
//cout<<"subtract\n";
tot-=(n-ind)*val+(n-tind)*th;
//cout<<tot<<'\n';
tot+=(n-tind)*val;
//cout<<tot<<'\n';
ind=tind;
s.pop();
} else {
break;
}
}
//cout<<i<<": "<<tot<<'\n';
s.push({ind,val});
}
cout<<tot<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
9980 KB |
Output is correct |
2 |
Correct |
118 ms |
9980 KB |
Output is correct |
3 |
Correct |
119 ms |
9976 KB |
Output is correct |
4 |
Correct |
112 ms |
10104 KB |
Output is correct |
5 |
Correct |
112 ms |
9976 KB |
Output is correct |
6 |
Correct |
120 ms |
9976 KB |
Output is correct |
7 |
Correct |
147 ms |
9972 KB |
Output is correct |
8 |
Correct |
113 ms |
9976 KB |
Output is correct |
9 |
Correct |
118 ms |
9976 KB |
Output is correct |
10 |
Correct |
118 ms |
9968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |