#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e6+3;
const ll big = 1e18;
struct obj{
ll i,h;
bool operator<(const obj &o) const{
return h<o.h;
}
};
struct segtree{
ll n;
vector<obj> a;
segtree(ll N):n(N),a(N<<2){}
void set(ll i, ll l, ll r, ll x, ll o){
if (l==r) a[i] = {l,o};
else{
ll m = l+r>>1;
if (x<=m) set(i<<1,l,m,x,o);
else set(i<<1|1,m+1,r,x,o);
a[i] = min(a[i<<1],a[i<<1|1]);
}
}
void set(ll x, ll o){
set(1,0,n-1,x,o);
}
obj get(ll i, ll l, ll r, ll ql, ll qr){
if (ql<=l&&r<=qr) return a[i];
ll m = l+r>>1;
if (qr<=m) return get(i<<1,l,m,ql,qr);
if (ql>m) return get(i<<1|1,m+1,r,ql,qr);
return min(get(i<<1,l,m,ql,qr),get(i<<1|1,m+1,r,ql,qr));
}
obj get(ll ql, ll qr){
return get(1,0,n-1,ql,qr);
}
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for(ll &i : a) cin >> i;
segtree t(n);
ll ans = 0;
for(ll z = 0;z<100;z++){
vector<obj> v;
for(ll i = 1;i<n-1;i++) if (a[i-1]>a[i]&&a[i]<a[i+1]) v.push_back({i,a[i]});
if (v.empty()) break;
sort(v.begin(),v.end());
for(ll i = 0;i<n;i++) t.set(i,a[i]<=v[0].h?big:a[i]);
for(ll i = 0;i<v.size()&&v[i].h==v[0].h;i++){
ll x = v[i].i;
ll l = t.get(0,x-1).h;
ll r = t.get(x+1,n-1).h;
ans += v[i].h+l+r;
a[x]++;
}
}
cout << ans << "\n";
}
Compilation message
Main.cpp: In member function 'void segtree::set(ll, ll, ll, ll, ll)':
Main.cpp:19:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | ll m = l+r>>1;
| ~^~
Main.cpp: In member function 'obj segtree::get(ll, ll, ll, ll, ll)':
Main.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | ll m = l+r>>1;
| ~^~
Main.cpp: In function 'int main()':
Main.cpp:53:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<obj>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(ll i = 0;i<v.size()&&v[i].h==v[0].h;i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |