#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){
if(idx==v.size())break;
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:41:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(idx==v.size())break;
| ~~~^~~~~~~~~~
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 |
17 ms |
2908 KB |
Output is correct |
2 |
Correct |
18 ms |
3020 KB |
Output is correct |
3 |
Correct |
28 ms |
2900 KB |
Output is correct |
4 |
Correct |
21 ms |
3036 KB |
Output is correct |
5 |
Correct |
21 ms |
3020 KB |
Output is correct |
6 |
Correct |
21 ms |
2980 KB |
Output is correct |
7 |
Correct |
18 ms |
2908 KB |
Output is correct |
8 |
Correct |
20 ms |
2908 KB |
Output is correct |
9 |
Correct |
18 ms |
2904 KB |
Output is correct |
10 |
Correct |
19 ms |
2908 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 |
12 ms |
2908 KB |
Output is correct |
4 |
Correct |
13 ms |
2908 KB |
Output is correct |
5 |
Correct |
25 ms |
2984 KB |
Output is correct |
6 |
Correct |
13 ms |
2904 KB |
Output is correct |
7 |
Correct |
13 ms |
2908 KB |
Output is correct |
8 |
Correct |
18 ms |
3040 KB |
Output is correct |
9 |
Correct |
17 ms |
2908 KB |
Output is correct |
10 |
Correct |
13 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2908 KB |
Output is correct |
2 |
Correct |
18 ms |
3020 KB |
Output is correct |
3 |
Correct |
28 ms |
2900 KB |
Output is correct |
4 |
Correct |
21 ms |
3036 KB |
Output is correct |
5 |
Correct |
21 ms |
3020 KB |
Output is correct |
6 |
Correct |
21 ms |
2980 KB |
Output is correct |
7 |
Correct |
18 ms |
2908 KB |
Output is correct |
8 |
Correct |
20 ms |
2908 KB |
Output is correct |
9 |
Correct |
18 ms |
2904 KB |
Output is correct |
10 |
Correct |
19 ms |
2908 KB |
Output is correct |
11 |
Correct |
14 ms |
2908 KB |
Output is correct |
12 |
Correct |
13 ms |
2908 KB |
Output is correct |
13 |
Correct |
12 ms |
2908 KB |
Output is correct |
14 |
Correct |
13 ms |
2908 KB |
Output is correct |
15 |
Correct |
25 ms |
2984 KB |
Output is correct |
16 |
Correct |
13 ms |
2904 KB |
Output is correct |
17 |
Correct |
13 ms |
2908 KB |
Output is correct |
18 |
Correct |
18 ms |
3040 KB |
Output is correct |
19 |
Correct |
17 ms |
2908 KB |
Output is correct |
20 |
Correct |
13 ms |
2908 KB |
Output is correct |
21 |
Incorrect |
40 ms |
12748 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |