#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
ll a[211111];
int n;
map<ii, ll> ma;
set<ii> intervals;
ll res[211111];
void solve_fast()
{
ma.clear(); intervals.clear();
multiset<pair<ll,ii> > pq;
for(int i=0;i<n;i++)
{
pq.insert(mp(a[i], mp(i,i)));
intervals.insert(mp(i,i));
ma[mp(i,i)] = a[i];
}
ll ans = 0;
for(int i=0;i<(n+1)/2;i++)
{
while(intervals.find((*prev(pq.end())).se)==intervals.end()) pq.erase(prev(pq.end()));
ans += (*prev(pq.end())).fi;
res[i+1] = ans;
int l = (*prev(pq.end())).se.fi; int r = (*prev(pq.end())).se.se;
auto it = intervals.find(mp(l,r));
auto itr = it; auto itl= it;
itr++; itl--;
vector<ii> era;
if(it!=intervals.begin()) era.pb((*itl));
if(itr!=intervals.end()) era.pb((*itr));
if(itr!=intervals.end()&&it!=intervals.begin())
{
int veryl = (*itl).fi;
int veryr = (*itr).se;
intervals.insert(mp(veryl,veryr));
ll V = ma[(*itl)] + ma[(*itr)] - ma[mp(l,r)];
ma[mp(veryl,veryr)] = V;
pq.insert(mp(V, mp(veryl, veryr)));
}
intervals.erase(mp(l,r));
for(ii x:era)
{
intervals.erase(x);
}
}
for(int i=1;i<=(n+1)/2;i++) cout<<res[i]<<'\n';
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
solve_fast();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
760 KB |
Output is correct |
2 |
Correct |
5 ms |
872 KB |
Output is correct |
3 |
Correct |
5 ms |
948 KB |
Output is correct |
4 |
Correct |
6 ms |
1044 KB |
Output is correct |
5 |
Correct |
5 ms |
1044 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
5 ms |
1112 KB |
Output is correct |
8 |
Correct |
5 ms |
1112 KB |
Output is correct |
9 |
Correct |
5 ms |
1112 KB |
Output is correct |
10 |
Correct |
5 ms |
1112 KB |
Output is correct |
11 |
Correct |
5 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
5 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1116 KB |
Output is correct |
15 |
Correct |
6 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
1144 KB |
Output is correct |
18 |
Correct |
5 ms |
1148 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
20 |
Correct |
5 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
760 KB |
Output is correct |
2 |
Correct |
5 ms |
872 KB |
Output is correct |
3 |
Correct |
5 ms |
948 KB |
Output is correct |
4 |
Correct |
6 ms |
1044 KB |
Output is correct |
5 |
Correct |
5 ms |
1044 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
5 ms |
1112 KB |
Output is correct |
8 |
Correct |
5 ms |
1112 KB |
Output is correct |
9 |
Correct |
5 ms |
1112 KB |
Output is correct |
10 |
Correct |
5 ms |
1112 KB |
Output is correct |
11 |
Correct |
5 ms |
1116 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
5 ms |
1116 KB |
Output is correct |
14 |
Correct |
5 ms |
1116 KB |
Output is correct |
15 |
Correct |
6 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
1144 KB |
Output is correct |
17 |
Correct |
5 ms |
1144 KB |
Output is correct |
18 |
Correct |
5 ms |
1148 KB |
Output is correct |
19 |
Correct |
5 ms |
1152 KB |
Output is correct |
20 |
Correct |
5 ms |
1156 KB |
Output is correct |
21 |
Correct |
1076 ms |
44180 KB |
Output is correct |
22 |
Correct |
1080 ms |
46012 KB |
Output is correct |
23 |
Correct |
1082 ms |
47456 KB |
Output is correct |
24 |
Correct |
593 ms |
47456 KB |
Output is correct |
25 |
Correct |
620 ms |
47456 KB |
Output is correct |
26 |
Correct |
628 ms |
47456 KB |
Output is correct |
27 |
Correct |
608 ms |
47456 KB |
Output is correct |
28 |
Correct |
645 ms |
47456 KB |
Output is correct |
29 |
Correct |
616 ms |
47456 KB |
Output is correct |
30 |
Correct |
637 ms |
47456 KB |
Output is correct |
31 |
Correct |
619 ms |
47456 KB |
Output is correct |
32 |
Correct |
618 ms |
47456 KB |
Output is correct |
33 |
Correct |
800 ms |
47456 KB |
Output is correct |
34 |
Correct |
796 ms |
47456 KB |
Output is correct |
35 |
Correct |
787 ms |
47456 KB |
Output is correct |
36 |
Correct |
1105 ms |
47456 KB |
Output is correct |
37 |
Correct |
1135 ms |
47456 KB |
Output is correct |
38 |
Correct |
1106 ms |
47456 KB |
Output is correct |
39 |
Correct |
626 ms |
47456 KB |
Output is correct |
40 |
Correct |
595 ms |
47556 KB |
Output is correct |
41 |
Correct |
597 ms |
47556 KB |
Output is correct |
42 |
Correct |
621 ms |
47556 KB |
Output is correct |
43 |
Correct |
630 ms |
47556 KB |
Output is correct |
44 |
Correct |
612 ms |
47556 KB |
Output is correct |
45 |
Correct |
613 ms |
47556 KB |
Output is correct |
46 |
Correct |
626 ms |
47556 KB |
Output is correct |
47 |
Correct |
689 ms |
47556 KB |
Output is correct |
48 |
Correct |
779 ms |
47556 KB |
Output is correct |
49 |
Correct |
798 ms |
47556 KB |
Output is correct |
50 |
Correct |
795 ms |
47556 KB |
Output is correct |