#include <bits/stdc++.h>
using namespace std;
#define oo 1e15
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) ((xxxx>>(aaaa-1))&1)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
const ll base=361;
const ll mod=998244353;
const ld eps=1e-5;
const ll maxn=1e6;
struct comp{
bool operator() (pll x,pll y) {
return x.fi>y.fi;
}
};
ll sig(ll x) {
if (x<0) {
return -1;
}
return 1;
}
priority_queue<pll,vector<pll>,comp> heap;
ll n,k,i,a[400009],tmp,b[400009],m,l[400009],r[400009],res,cnt,cur;
bool ok[400009];
int main(){
IO;
cin>>n>>k;
//cout<<n<<'\n';
for (i=1;i<=n;i++) {
cin>>a[i];
}
for (i=1;i<=n;i++) {
if (sig(a[i])*sig(a[i-1])<0) {
if (m==0&&cur<0) {
cur=a[i];
}
else {
//cout<<i<<'\n';
m++;
b[m]=cur;
cur=a[i];
}
}
else {
cur+=a[i];
}
//cout<<cur<<'\n';
}
if (cur>0) {
m++;
b[m]=cur;
}
//cout<<m<<'\n';
for (i=1;i<=m;i++) {
//cout<<b[i]<<' ';
l[i]=i+1;
r[i]=i-1;
if (b[i]>0) {
cnt++;
}
heap.push({abs(b[i]),i});
}
//cout<<'\n';
r[1]=0;
l[m]=0;
l[0]=1;
r[m+1]=m;
while (cnt>k) {
while (b[l[0]]<0) {
ok[l[0]]=true;
l[0]=l[l[0]];
r[l[0]]=0;
}
while (b[r[m+1]]<0) {
ok[r[m+1]]=true;
r[m+1]=r[r[m+1]];
l[r[m+1]]=0;
}
ll y=heap.top().fi;
ll x=heap.top().se;
heap.pop();
if (ok[x]) continue;
//cout<<b[x]<<' '<<l[x]<<' '<<r[x]<<'\n';
tmp=0;
if (b[x]>0) {
tmp++;
}
if (b[r[x]]>0) {
tmp++;
}
if (b[l[x]]>0) {
tmp++;
}
if (b[x]+b[l[x]]+b[r[x]]>0) {
tmp--;
}
cnt-=tmp;
ok[l[x]]=true;
ok[r[x]]=true;
b[x]=b[l[x]]+b[r[x]]+b[x];
l[x]=l[l[x]];
r[x]=r[r[x]];
if (l[x]!=0) {
r[l[x]]=x;
}
if (r[x]!=0) {
l[r[x]]=x;
}
//if (b[x]>=0) {
heap.push({abs(b[x]),x});
//}
}
for (i=1;i<=m;i++) {
if (!ok[i]&&b[i]>0) {
res+=b[i];
}
}
cout<<res<<'\n';
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:99:12: warning: unused variable 'y' [-Wunused-variable]
99 | ll y=heap.top().fi;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
5388 KB |
Output is correct |
2 |
Correct |
35 ms |
5508 KB |
Output is correct |
3 |
Correct |
37 ms |
5504 KB |
Output is correct |
4 |
Correct |
35 ms |
5456 KB |
Output is correct |
5 |
Correct |
35 ms |
5444 KB |
Output is correct |
6 |
Correct |
35 ms |
5364 KB |
Output is correct |
7 |
Correct |
34 ms |
5364 KB |
Output is correct |
8 |
Correct |
37 ms |
5440 KB |
Output is correct |
9 |
Correct |
34 ms |
5328 KB |
Output is correct |
10 |
Correct |
34 ms |
5444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3788 KB |
Output is correct |
2 |
Correct |
28 ms |
3780 KB |
Output is correct |
3 |
Correct |
23 ms |
3760 KB |
Output is correct |
4 |
Correct |
23 ms |
3648 KB |
Output is correct |
5 |
Correct |
34 ms |
5316 KB |
Output is correct |
6 |
Correct |
25 ms |
3660 KB |
Output is correct |
7 |
Correct |
24 ms |
3792 KB |
Output is correct |
8 |
Correct |
35 ms |
5540 KB |
Output is correct |
9 |
Correct |
34 ms |
5316 KB |
Output is correct |
10 |
Correct |
24 ms |
3744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
13140 KB |
Output is correct |
2 |
Correct |
89 ms |
13060 KB |
Output is correct |
3 |
Correct |
95 ms |
13116 KB |
Output is correct |
4 |
Correct |
86 ms |
12992 KB |
Output is correct |
5 |
Correct |
86 ms |
13116 KB |
Output is correct |
6 |
Correct |
87 ms |
13116 KB |
Output is correct |
7 |
Correct |
89 ms |
13144 KB |
Output is correct |
8 |
Correct |
93 ms |
13072 KB |
Output is correct |
9 |
Correct |
91 ms |
13204 KB |
Output is correct |
10 |
Correct |
90 ms |
13112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
5388 KB |
Output is correct |
2 |
Correct |
35 ms |
5508 KB |
Output is correct |
3 |
Correct |
37 ms |
5504 KB |
Output is correct |
4 |
Correct |
35 ms |
5456 KB |
Output is correct |
5 |
Correct |
35 ms |
5444 KB |
Output is correct |
6 |
Correct |
35 ms |
5364 KB |
Output is correct |
7 |
Correct |
34 ms |
5364 KB |
Output is correct |
8 |
Correct |
37 ms |
5440 KB |
Output is correct |
9 |
Correct |
34 ms |
5328 KB |
Output is correct |
10 |
Correct |
34 ms |
5444 KB |
Output is correct |
11 |
Correct |
24 ms |
3788 KB |
Output is correct |
12 |
Correct |
28 ms |
3780 KB |
Output is correct |
13 |
Correct |
23 ms |
3760 KB |
Output is correct |
14 |
Correct |
23 ms |
3648 KB |
Output is correct |
15 |
Correct |
34 ms |
5316 KB |
Output is correct |
16 |
Correct |
25 ms |
3660 KB |
Output is correct |
17 |
Correct |
24 ms |
3792 KB |
Output is correct |
18 |
Correct |
35 ms |
5540 KB |
Output is correct |
19 |
Correct |
34 ms |
5316 KB |
Output is correct |
20 |
Correct |
24 ms |
3744 KB |
Output is correct |
21 |
Correct |
87 ms |
13140 KB |
Output is correct |
22 |
Correct |
89 ms |
13060 KB |
Output is correct |
23 |
Correct |
95 ms |
13116 KB |
Output is correct |
24 |
Correct |
86 ms |
12992 KB |
Output is correct |
25 |
Correct |
86 ms |
13116 KB |
Output is correct |
26 |
Correct |
87 ms |
13116 KB |
Output is correct |
27 |
Correct |
89 ms |
13144 KB |
Output is correct |
28 |
Correct |
93 ms |
13072 KB |
Output is correct |
29 |
Correct |
91 ms |
13204 KB |
Output is correct |
30 |
Correct |
90 ms |
13112 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
320 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
324 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
324 KB |
Output is correct |
45 |
Correct |
1 ms |
320 KB |
Output is correct |
46 |
Correct |
1 ms |
312 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |