#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FastIO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define lg 31
#define mxn 100005
set<pair<int,int>> mx, st;
void erase(pair<int,int> p){
mx.erase(mx.find({p.second,p.first}));
st.erase(st.find(p));
}
int f(int cur){
if (st.size()==0) return 0;
if (st.size()==1){
cout<<(*st.begin()).second+cur<<"\n";
return (*st.begin()).second;
}
auto it=--mx.end();
pair<int,int> tmp=*it;
auto it2=st.find({tmp.second,tmp.first});
int res=tmp.first;
if (it2==st.begin()){
erase(*(it2++));
} else if (it2==--st.end()){
erase(*(it2--));
} else{
int x=((*(--it2)).second)-((*(++it2)).second)+((*(++it2)).second);
erase(*(it2--));
erase(*(it2--));
mx.insert({x,tmp.second});
st.insert({tmp.second,x});
}
erase(*it2);
cout<<res+cur<<"\n";
res+=f(res+cur);
return res;
}
signed main(){
FastIO
int tc=1;
// cin >> tc;
while (tc--){
int n;
cin>>n;
FOR(i,1,n){
int t;cin>>t;
st.insert({i,t});
mx.insert({t,i});
}
f(0);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
660 KB |
Output is correct |
5 |
Correct |
3 ms |
1036 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
660 KB |
Output is correct |
5 |
Correct |
3 ms |
1036 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
495 ms |
28892 KB |
Output is correct |
22 |
Correct |
462 ms |
28752 KB |
Output is correct |
23 |
Correct |
546 ms |
28988 KB |
Output is correct |
24 |
Correct |
212 ms |
28568 KB |
Output is correct |
25 |
Correct |
211 ms |
28600 KB |
Output is correct |
26 |
Correct |
206 ms |
28552 KB |
Output is correct |
27 |
Correct |
224 ms |
28864 KB |
Output is correct |
28 |
Correct |
214 ms |
28752 KB |
Output is correct |
29 |
Correct |
210 ms |
28760 KB |
Output is correct |
30 |
Correct |
237 ms |
28824 KB |
Output is correct |
31 |
Correct |
230 ms |
28760 KB |
Output is correct |
32 |
Correct |
239 ms |
28820 KB |
Output is correct |
33 |
Correct |
325 ms |
28596 KB |
Output is correct |
34 |
Correct |
396 ms |
28628 KB |
Output is correct |
35 |
Correct |
331 ms |
28568 KB |
Output is correct |
36 |
Correct |
528 ms |
28808 KB |
Output is correct |
37 |
Correct |
536 ms |
28884 KB |
Output is correct |
38 |
Correct |
493 ms |
28752 KB |
Output is correct |
39 |
Correct |
211 ms |
28604 KB |
Output is correct |
40 |
Correct |
223 ms |
28760 KB |
Output is correct |
41 |
Correct |
208 ms |
28752 KB |
Output is correct |
42 |
Correct |
214 ms |
28796 KB |
Output is correct |
43 |
Correct |
213 ms |
28752 KB |
Output is correct |
44 |
Correct |
208 ms |
28772 KB |
Output is correct |
45 |
Correct |
234 ms |
28684 KB |
Output is correct |
46 |
Correct |
242 ms |
28760 KB |
Output is correct |
47 |
Correct |
253 ms |
28756 KB |
Output is correct |
48 |
Correct |
350 ms |
28636 KB |
Output is correct |
49 |
Correct |
405 ms |
28620 KB |
Output is correct |
50 |
Correct |
360 ms |
28720 KB |
Output is correct |