#include <bits/stdc++.h>
#define N 200005
#define inf 100000000000000000LL
#define ll long long
#define int long long
#define ii pair <int,long long>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
const unsigned ll Mod = 1000000007LL;
int n, k, a[N];
void init(){
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
}
bool ifsub1(){
for (int i = 1; i <= n; i++)
if (a[i] < 0) return 0;
return 1;
}
void sub1(){
int ans = 0;
for (int i = 1; i <= n; i++)
ans += a[i];
cout << ans << '\n';
}
int pos;
bool ifsub2(){
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] < 0) {pos = a[i]; cnt++;}
return (cnt < 2);
}
void sub2(){
int sum = 0, suml = 0, am = 0;
bool kt = 0;
for (int i = 1; i <= n; i++){
if (a[i] < 0) {
kt = 1;
am = a[i];
}
if (!kt) suml += a[i];
sum += a[i];
}
if (k > 1)
cout << sum - am << '\n';
else cout << max(suml, max(sum, sum - suml - am));
}
void sub3(){
int ans = 0, res = 0;
for (int i = 1; i <= n; i++) {
res = max(0ll, res + a[i]);
ans = max(ans, res);
}
cout << ans << '\n';
}
//int pa[N], sz[N];
//int findpa(int u){
//
// if (u == pa[u]) return u;
// return pa[u] = findpa(pa[u]);
//}
//void Union(int a, int b){
//
// a = findpa(a);
// b = findpa(b);
// if (a == b) return;
// if (sz[a] < sz[b]) swap(a,b);
// pa[b] = a;
// sz[a] += sz[b];
// sum[a] += sum[b];
//}
//vector <ii> d;
//void Add(int u){
// d.pb({u, sum[u]});
//}
//void sub6(){
// for (int i = 1; i <= n; i++){
// pa[i] = i;
// sz[i] = 1;
// sum[i] = a[i];
// }
// int parduong, param;
// bool task = 0;
// if (a[1] >= 0) {parduong = 1; task = 1;}
// else param = 1;
// for (int i = 1; i <= n; i++){
// if (a[i] >= 0) {
// if (task)
// Union(parduong, i);
// else parduong = i;
// task = 1;
// }
// else {
// if (!task) Union(param, i);
// else param = i;
// task = 0;
// }
// }
// int curpa = findpa(1);
// Add(curpa);
// for (int i = 1; i <= n; i++){
// int u = findpa(i);
// if (u != curpa){
// curpa = u;
// Add(u);
// }
// }
// for (int i = 2; i < d.size(); i++){
// if (d[i].se > 0 && d[i-2].se > 0){
// g.pb({d[i].fi, d[i - 2].fi, d[i].se + d[i].se });
// }
// }
//}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
#define nametask "PRESUFF"
if(fopen(nametask".inp", "r")) {
freopen(nametask".inp", "r", stdin);
freopen(nametask".out", "w", stdout);
}
init();
if (ifsub1()) return sub1(), 0;
if (ifsub2()) return sub2(), 0;
if (k == 1) return sub3(), 0;
// if (k <= n && n <= 2000) return sub6(), 0;
return 0;
}
/// Duong Anh Kiet from Kien Giang ///
/*
6 1
-1 -2 -1 2 -1 2
*/
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(nametask".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(nametask".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
3664 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
1872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
2012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
3664 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |