#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define ii pair<ll, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}
const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 3e5 + 10, M = 18;
const long long INF = (long long)4e18 + 11;
/*
/\_/\
(= ._.)
/ >? \>$
*/
int n, k;
int A[N], L[N], R[N];
long long b[N];
int sign(int x){
if(x == 0) return 0;
return (x > 0 ? 1 : -1);
}
void sol(void){
cin >> n >> k;
FOR(i, 1, n) cin >> A[i];
int m = 0, i = 1;
while(A[i] <= 0) ++i;
while(i <= n){
++m;
int j = i;
while(j <= n && sign(A[i]) * sign(A[j]) >= 0) b[m] += A[j++];
i = j;
}
if(b[m] <= 0) m--;
// FOR(i, 1, m) cout << b[i] << " \n"[i == m];
set<ii> s;
FOR(i, 2, m) L[i] = i-1;
FOR(i, 1, m-1) R[i] = i+1;
FOR(i, 1, m) s.insert(make_pair(abs(b[i]), i));
int cnt = m/2 + 1;
while(cnt > k){
long long v, p; tie(v, p) = *s.begin();
int l = L[p], r = R[p];
s.erase(s.begin());
if(l > 0) s.erase({abs(b[l]), l});
if(r > 0) s.erase({abs(b[r]), r});
if(v > 0 || (l > 0 && r > 0)) cnt--;
b[p] += b[l] + b[r];
b[l] = b[r] = 0;
if(l > 0) R[L[l]] = p, L[p] = L[l];
if(r > 0) L[R[r]] = p, R[p] = R[r];
if(b[p] <= 0 && (l == 0 || r == 0)) b[p] = R[L[l]] = L[R[r]] = 0;
else s.insert({abs(b[p]), p});
}
long long res = 0;
FOR(i, 1, m) if(b[i] > 0) res += b[i];
cout << res << '\n';
}
signed main(void){
#define TASK "BANQUET"
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--) sol();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |