#include <iostream>
#include <queue>
#include <set>
#define ll long long
#define pii pair<int,int>
#define fst first
#define snd second
#define ppp pair<pair<ll,int>,pii>
using namespace std;
int n,k,l=-1;ll ar[300001]={},to=0;set<pii> s;
priority_queue<ppp> pq;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for (int i=1;i<=n;i++)
{
cin>>ar[i];
if (ar[i]>=0)
{
if (l==-1) {l = i;}
}
else
{
if (l!=-1) {to+=ar[i-1] - ar[l-1];s.insert({l,i-1});l=-1;}
}
ar[i] += ar[i-1];
}
if (l!=-1) {s.insert({l,n});to+=ar[n] - ar[l-1];}
l = -1;
for (auto dt:s)
{
pq.push({{-(ar[dt.snd] - ar[dt.fst-1]),0},{dt.fst,dt.snd}});
if (l!=-1)
{
pq.push({{ar[dt.fst-1] - ar[l],1},{l+1,dt.fst-1}});
}
l = dt.snd;
}
while (s.size()>k)
{
while (pq.size())
{
if (pq.top().fst.snd)
{
auto it = s.lower_bound({pq.top().snd.snd,pq.top().snd.snd});
pii dt = {-1,-1},dt2 = {-1,-1};
if (it!=s.begin()) {dt2 = *prev(it);}
if (it!=s.end()) {dt = *it;}
if (make_pair(dt2.snd+1,dt.fst-1)!=pq.top().snd) {pq.pop();}
else {break;}
}
else
{
auto it = s.lower_bound({pq.top().snd.fst,pq.top().snd.snd});
pii dt = {-1,-1};
if (it!=s.end()) {dt = *it;}
if (dt!=pq.top().snd) {pq.pop();}
else {break;}
}
}
to += pq.top().fst.fst;
if (pq.top().fst.snd)
{
auto it = s.lower_bound(pq.top().snd);
int l=prev(it)->fst,r=it->snd;
s.erase(prev(it));s.erase(it);pq.pop();
s.insert({l,r});
pq.push({{-(ar[r] - ar[l-1]),0},{l,r}});
}
else
{
auto it = s.lower_bound(pq.top().snd);
int l=pq.top().snd.fst,r=pq.top().snd.snd;
if (next(it)!=s.end()) {r = next(it)->fst-1;}
if (it!=s.begin()) {l = prev(it)->snd+1;}
s.erase(pq.top().snd);pq.pop();
pq.push({{ar[r] - ar[l-1],1},{l,r}});
}
}
cout<<to<<"\n";
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:40:17: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | while (s.size()>k)
| ~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
5484 KB |
Output is correct |
2 |
Correct |
37 ms |
5484 KB |
Output is correct |
3 |
Correct |
37 ms |
5740 KB |
Output is correct |
4 |
Correct |
36 ms |
5484 KB |
Output is correct |
5 |
Correct |
36 ms |
5484 KB |
Output is correct |
6 |
Correct |
38 ms |
5356 KB |
Output is correct |
7 |
Correct |
35 ms |
5484 KB |
Output is correct |
8 |
Correct |
39 ms |
5484 KB |
Output is correct |
9 |
Correct |
39 ms |
5396 KB |
Output is correct |
10 |
Correct |
41 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3816 KB |
Output is correct |
2 |
Correct |
30 ms |
3820 KB |
Output is correct |
3 |
Correct |
27 ms |
3692 KB |
Output is correct |
4 |
Correct |
28 ms |
3820 KB |
Output is correct |
5 |
Correct |
38 ms |
5484 KB |
Output is correct |
6 |
Correct |
23 ms |
3692 KB |
Output is correct |
7 |
Correct |
24 ms |
3840 KB |
Output is correct |
8 |
Correct |
36 ms |
5484 KB |
Output is correct |
9 |
Correct |
40 ms |
5484 KB |
Output is correct |
10 |
Correct |
24 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
15728 KB |
Output is correct |
2 |
Correct |
233 ms |
15328 KB |
Output is correct |
3 |
Correct |
222 ms |
15452 KB |
Output is correct |
4 |
Correct |
235 ms |
15324 KB |
Output is correct |
5 |
Correct |
230 ms |
15452 KB |
Output is correct |
6 |
Correct |
230 ms |
15452 KB |
Output is correct |
7 |
Correct |
239 ms |
15452 KB |
Output is correct |
8 |
Correct |
233 ms |
15584 KB |
Output is correct |
9 |
Correct |
235 ms |
15584 KB |
Output is correct |
10 |
Correct |
244 ms |
15584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 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 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 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 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
5484 KB |
Output is correct |
2 |
Correct |
37 ms |
5484 KB |
Output is correct |
3 |
Correct |
37 ms |
5740 KB |
Output is correct |
4 |
Correct |
36 ms |
5484 KB |
Output is correct |
5 |
Correct |
36 ms |
5484 KB |
Output is correct |
6 |
Correct |
38 ms |
5356 KB |
Output is correct |
7 |
Correct |
35 ms |
5484 KB |
Output is correct |
8 |
Correct |
39 ms |
5484 KB |
Output is correct |
9 |
Correct |
39 ms |
5396 KB |
Output is correct |
10 |
Correct |
41 ms |
5484 KB |
Output is correct |
11 |
Correct |
28 ms |
3816 KB |
Output is correct |
12 |
Correct |
30 ms |
3820 KB |
Output is correct |
13 |
Correct |
27 ms |
3692 KB |
Output is correct |
14 |
Correct |
28 ms |
3820 KB |
Output is correct |
15 |
Correct |
38 ms |
5484 KB |
Output is correct |
16 |
Correct |
23 ms |
3692 KB |
Output is correct |
17 |
Correct |
24 ms |
3840 KB |
Output is correct |
18 |
Correct |
36 ms |
5484 KB |
Output is correct |
19 |
Correct |
40 ms |
5484 KB |
Output is correct |
20 |
Correct |
24 ms |
3820 KB |
Output is correct |
21 |
Correct |
261 ms |
15728 KB |
Output is correct |
22 |
Correct |
233 ms |
15328 KB |
Output is correct |
23 |
Correct |
222 ms |
15452 KB |
Output is correct |
24 |
Correct |
235 ms |
15324 KB |
Output is correct |
25 |
Correct |
230 ms |
15452 KB |
Output is correct |
26 |
Correct |
230 ms |
15452 KB |
Output is correct |
27 |
Correct |
239 ms |
15452 KB |
Output is correct |
28 |
Correct |
233 ms |
15584 KB |
Output is correct |
29 |
Correct |
235 ms |
15584 KB |
Output is correct |
30 |
Correct |
244 ms |
15584 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
492 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
492 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
492 KB |
Output is correct |
52 |
Correct |
1 ms |
492 KB |
Output is correct |
53 |
Correct |
1 ms |
492 KB |
Output is correct |
54 |
Correct |
1 ms |
492 KB |
Output is correct |
55 |
Correct |
1 ms |
492 KB |
Output is correct |
56 |
Correct |
1 ms |
492 KB |
Output is correct |
57 |
Correct |
1 ms |
492 KB |
Output is correct |
58 |
Correct |
1 ms |
492 KB |
Output is correct |
59 |
Correct |
2 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
101 ms |
15352 KB |
Output is correct |
62 |
Correct |
77 ms |
15580 KB |
Output is correct |
63 |
Correct |
204 ms |
15328 KB |
Output is correct |
64 |
Correct |
107 ms |
15456 KB |
Output is correct |
65 |
Correct |
161 ms |
15452 KB |
Output is correct |
66 |
Correct |
153 ms |
15452 KB |
Output is correct |
67 |
Correct |
152 ms |
15456 KB |
Output is correct |
68 |
Correct |
252 ms |
15484 KB |
Output is correct |
69 |
Correct |
253 ms |
15324 KB |
Output is correct |
70 |
Correct |
223 ms |
15200 KB |
Output is correct |