#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const ll mxn = 1e6+10;
vector<pll> v;
ll sum[mxn],dp;
deque<pll> dq;
int n;
ll total = 0;
inline ll calc(pll line,ll val){
return line.fs*val+line.sc;
}
inline bool out(pll a,pll b,pll c){
return (a.sc-b.sc)*(c.fs-b.fs)>=(b.sc-c.sc)*(b.fs-a.fs);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 0;i<n;i++){
int k;
cin>>k;
if(v.empty()||v.back().fs < k)v.push_back({k,1});
else v.back().sc++;
}
dq.push_back(make_pair(0ll,0ll));
sum[0] = v[0].sc;
for(int i = 1;i<v.size();i++){
sum[i] = sum[i-1]+v[i].sc;
}
for(int i = 0;i<v.size();i++){
pll p = v[i];
while(dq.size()>1&&calc(dq[1],p.fs)<=calc(dq[0],p.fs))dq.pop_front();
dp = p.fs*n+calc(dq.front(),p.fs);
pll now = make_pair(-sum[i],dp);
while(dq.size()>1&&out(dq.end()[-2],dq.end()[-2],now))dq.pop_back();
dq.push_back(make_pair(-sum[i],dp));
}
cout<<dp;
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:37:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
Discharging.cpp:40:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
8024 KB |
Output is correct |
2 |
Correct |
74 ms |
8104 KB |
Output is correct |
3 |
Correct |
72 ms |
8016 KB |
Output is correct |
4 |
Correct |
77 ms |
8268 KB |
Output is correct |
5 |
Correct |
72 ms |
8020 KB |
Output is correct |
6 |
Correct |
77 ms |
8108 KB |
Output is correct |
7 |
Correct |
73 ms |
8120 KB |
Output is correct |
8 |
Correct |
72 ms |
8020 KB |
Output is correct |
9 |
Correct |
73 ms |
8100 KB |
Output is correct |
10 |
Correct |
72 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |