#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
ll slope(pll x, pll y){
if(x.first<y.first){
swap(x,y);
}
return(y.second-x.second)/(x.first-y.first);
}
int main(){
ios_base::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
int n;
cin >> n;
vector<ll> v(n+5), dp(n+5);
vector<pair<ll,ll>> q;
for(int i = 1; i <= n; i++){
cin >> v[i];
v[i] = max(v[i], v[i-1]);
}
q.emplace_back(n,0);
int idx=0;
for(int i = 1; i <= n; i++){
dp[i] = (ll)1e9;
if(v[i]==v[i+1]) continue;
while(idx+1 < (int)q.size() and slope(q[idx],q[idx+1]) < v[i]){
idx++;
}
dp[i] = dp[idx] + q[idx].first * v[i];
while(q.size() > 1 and slope(q[q.size()-2], q[q.size()-1]) >= slope(q[q.size()-1], make_pair((ll)n-i, (ll)dp[i]))){
q.pop_back();
}
q.emplace_back(n - i, dp[i]);
}
cout << dp[n] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
16632 KB |
Output is correct |
2 |
Correct |
127 ms |
16232 KB |
Output is correct |
3 |
Correct |
127 ms |
16228 KB |
Output is correct |
4 |
Correct |
121 ms |
16356 KB |
Output is correct |
5 |
Correct |
135 ms |
16356 KB |
Output is correct |
6 |
Correct |
124 ms |
16228 KB |
Output is correct |
7 |
Correct |
127 ms |
16356 KB |
Output is correct |
8 |
Correct |
123 ms |
16236 KB |
Output is correct |
9 |
Correct |
129 ms |
16356 KB |
Output is correct |
10 |
Correct |
126 ms |
16228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |