#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,k;
cin>>n>>k;
ll a[n];
for(int i=0;i<n;i++)cin>>a[i];
vector<pair<pll,pll>> v;
ll sum=0,s=a[0]>=0,l=0,cnt=0,ans=0;
for(int i=0;i<n;i++){
if(s^(a[i]>=0)){
v.pb({{abs(sum),s},{l,i-1}});
if(s){
cnt++;
ans+=sum;
}
s=!s;
sum=0;
l=i;
}
sum+=a[i];
}
v.pb({{abs(sum),s},{l,n-1}});
if(s){
cnt++;
ans+=sum;
}
bool b[n];
for(int i=0;i<n;i++)b[i]=a[i]>=0;
sort(v.begin(),v.end());
int idx=0;
while(cnt>k){
auto[x,y]=v[idx].fs;
auto[l,r]=v[idx].sc;
idx++;
if(y){
if(l>0&&b[l-1]||r<n-1&&b[r+1])continue;
for(int i=l;i<=r;i++)b[i]=0;
ans-=x;
}else{
if(!l||!b[l-1]||r==n-1||!b[r+1])continue;
for(int i=l;i<=r;i++)b[i]=1;
ans-=x;
}
cnt--;
}
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:46:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
46 | if(l>0&&b[l-1]||r<n-1&&b[r+1])continue;
| ~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2908 KB |
Output is correct |
2 |
Correct |
19 ms |
3028 KB |
Output is correct |
3 |
Correct |
21 ms |
3032 KB |
Output is correct |
4 |
Correct |
20 ms |
3036 KB |
Output is correct |
5 |
Correct |
19 ms |
2908 KB |
Output is correct |
6 |
Correct |
19 ms |
2984 KB |
Output is correct |
7 |
Correct |
19 ms |
2980 KB |
Output is correct |
8 |
Correct |
20 ms |
2896 KB |
Output is correct |
9 |
Correct |
19 ms |
2908 KB |
Output is correct |
10 |
Correct |
23 ms |
3012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2908 KB |
Output is correct |
2 |
Correct |
13 ms |
2908 KB |
Output is correct |
3 |
Correct |
13 ms |
2996 KB |
Output is correct |
4 |
Correct |
13 ms |
3020 KB |
Output is correct |
5 |
Correct |
21 ms |
2984 KB |
Output is correct |
6 |
Correct |
13 ms |
2992 KB |
Output is correct |
7 |
Correct |
13 ms |
2908 KB |
Output is correct |
8 |
Correct |
21 ms |
3040 KB |
Output is correct |
9 |
Correct |
19 ms |
2904 KB |
Output is correct |
10 |
Correct |
13 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
43 ms |
16128 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2908 KB |
Output is correct |
2 |
Correct |
19 ms |
3028 KB |
Output is correct |
3 |
Correct |
21 ms |
3032 KB |
Output is correct |
4 |
Correct |
20 ms |
3036 KB |
Output is correct |
5 |
Correct |
19 ms |
2908 KB |
Output is correct |
6 |
Correct |
19 ms |
2984 KB |
Output is correct |
7 |
Correct |
19 ms |
2980 KB |
Output is correct |
8 |
Correct |
20 ms |
2896 KB |
Output is correct |
9 |
Correct |
19 ms |
2908 KB |
Output is correct |
10 |
Correct |
23 ms |
3012 KB |
Output is correct |
11 |
Correct |
14 ms |
2908 KB |
Output is correct |
12 |
Correct |
13 ms |
2908 KB |
Output is correct |
13 |
Correct |
13 ms |
2996 KB |
Output is correct |
14 |
Correct |
13 ms |
3020 KB |
Output is correct |
15 |
Correct |
21 ms |
2984 KB |
Output is correct |
16 |
Correct |
13 ms |
2992 KB |
Output is correct |
17 |
Correct |
13 ms |
2908 KB |
Output is correct |
18 |
Correct |
21 ms |
3040 KB |
Output is correct |
19 |
Correct |
19 ms |
2904 KB |
Output is correct |
20 |
Correct |
13 ms |
2904 KB |
Output is correct |
21 |
Runtime error |
43 ms |
16128 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |