#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
const int N=3e5+10;
ll n, k, a[N];
int main()
{
setIO();
cin >> n >> k;
FOR(i, 0, n) cin >> a[i];
vector<ll> nw;
bool tg=a[0]>0;
ll now=0;
FOR(i, 0, n)
{
if(a[i]==0) continue;
if(a[i]>0 && !tg){
tg=true;
if(now!=0)
nw.pb(now);
now=a[i];
}
else if(a[i]<0 && tg){
tg=false;
if(now!=0)
nw.pb(now);
now=a[i];
}
else{
now+=a[i];
}
}
nw.pb(now);
n=sz(nw);
FOR(i, 0, n) a[i]=nw[i];
FOR(i, 0, n)
{
if(a[i]<0){
if(i!=0) a[i]=max(-a[i-1], a[i]);
if(i!=n-1) a[i]=max(-a[i+1], a[i]);
}
}
ll sm=0;
vector<ll> negs;
FOR(i, 0, n)
{
if(a[i]<0) negs.pb(a[i]);
sm+=a[i];
}
sort(all(negs));
FOR(i, 0, k)
{
if(i>=sz(negs)) break;
sm-=negs[i];
}
cout << sm;
}
Compilation message
feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2796 KB |
Output is correct |
2 |
Correct |
38 ms |
2932 KB |
Output is correct |
3 |
Correct |
33 ms |
2948 KB |
Output is correct |
4 |
Correct |
33 ms |
2896 KB |
Output is correct |
5 |
Correct |
37 ms |
2816 KB |
Output is correct |
6 |
Correct |
29 ms |
2880 KB |
Output is correct |
7 |
Correct |
28 ms |
2892 KB |
Output is correct |
8 |
Correct |
31 ms |
2884 KB |
Output is correct |
9 |
Correct |
34 ms |
2956 KB |
Output is correct |
10 |
Correct |
31 ms |
2872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
2884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
5696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2796 KB |
Output is correct |
2 |
Correct |
38 ms |
2932 KB |
Output is correct |
3 |
Correct |
33 ms |
2948 KB |
Output is correct |
4 |
Correct |
33 ms |
2896 KB |
Output is correct |
5 |
Correct |
37 ms |
2816 KB |
Output is correct |
6 |
Correct |
29 ms |
2880 KB |
Output is correct |
7 |
Correct |
28 ms |
2892 KB |
Output is correct |
8 |
Correct |
31 ms |
2884 KB |
Output is correct |
9 |
Correct |
34 ms |
2956 KB |
Output is correct |
10 |
Correct |
31 ms |
2872 KB |
Output is correct |
11 |
Incorrect |
20 ms |
2884 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |