#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define el cout <<"\n"
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&(1LL))
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
const int N=1e6+5;
const int base=311;
const int mod=1e9+7;
const long long INF=1e18+7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
void add(int &x, int y) { x += y; if (x>=mod) x-=mod;}
void sub(int &x, int y) { x -= y; if (x<0) x+=mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
int calPw(int x, int y)
{
int ans = 1;
while(y)
{
if (y&1) ans = 1LL * ans * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ans;
}
///KhengmepfromLTVHSFTG
int n,a[N],k;
ll pre[N],dp[5005][5005];
bool check=true,check2=true;
void khengmep()
{
if(check==true)
{
ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i];
}
cout<<sum;
return;
}
if(check2==true)
{
if(k==1)
{
for(int i=1;i<=n;i++)
{
if(a[i]<0)
{
cout<<max(pre[i-1],pre[n]-pre[i-1]-a[i]);
return;
}
}
}
for(int i=1;i<=n;i++)
{
if(a[i]<0)
{
cout<<pre[n]-a[i];
return;
}
}
}
for(int i=1;i<=k;i++)
{
ll maxx=-INF;
for(int j=1;j<=n;j++)
{
maxx=max(maxx,dp[j-1][i-1]-pre[j-1]);
dp[j][i]=max(maxx+pre[j],dp[j-1][i]);
}
}
cout<<dp[n][k];
}
void ip()
{
cin>>n>>k;
pre[0]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(check==false&&a[i]<0) check2=false;
if(a[i]<0) check=false;
pre[i]=pre[i-1]+a[i];
}
}
int main()
{
// freopen("BANQUET.inp","r",stdin);
// freopen("BANQUET.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(0);
int t=1;
//cin>>t;
while(t--)
{
ip();
khengmep();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6480 KB |
Output is correct |
2 |
Correct |
22 ms |
6480 KB |
Output is correct |
3 |
Correct |
21 ms |
6480 KB |
Output is correct |
4 |
Correct |
21 ms |
6480 KB |
Output is correct |
5 |
Correct |
22 ms |
6480 KB |
Output is correct |
6 |
Correct |
31 ms |
6480 KB |
Output is correct |
7 |
Correct |
21 ms |
6480 KB |
Output is correct |
8 |
Correct |
21 ms |
6628 KB |
Output is correct |
9 |
Correct |
22 ms |
6480 KB |
Output is correct |
10 |
Correct |
23 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
14 ms |
6480 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
221 ms |
212184 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8680 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8544 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8680 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8544 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16736 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8680 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8544 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16736 KB |
Output is correct |
19 |
Correct |
2 ms |
16732 KB |
Output is correct |
20 |
Correct |
2 ms |
16732 KB |
Output is correct |
21 |
Correct |
10 ms |
66396 KB |
Output is correct |
22 |
Correct |
26 ms |
73308 KB |
Output is correct |
23 |
Correct |
11 ms |
73812 KB |
Output is correct |
24 |
Correct |
12 ms |
66396 KB |
Output is correct |
25 |
Correct |
12 ms |
63592 KB |
Output is correct |
26 |
Correct |
9 ms |
57696 KB |
Output is correct |
27 |
Correct |
11 ms |
63740 KB |
Output is correct |
28 |
Correct |
9 ms |
66140 KB |
Output is correct |
29 |
Correct |
8 ms |
62300 KB |
Output is correct |
30 |
Correct |
8 ms |
58368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6480 KB |
Output is correct |
2 |
Correct |
22 ms |
6480 KB |
Output is correct |
3 |
Correct |
21 ms |
6480 KB |
Output is correct |
4 |
Correct |
21 ms |
6480 KB |
Output is correct |
5 |
Correct |
22 ms |
6480 KB |
Output is correct |
6 |
Correct |
31 ms |
6480 KB |
Output is correct |
7 |
Correct |
21 ms |
6480 KB |
Output is correct |
8 |
Correct |
21 ms |
6628 KB |
Output is correct |
9 |
Correct |
22 ms |
6480 KB |
Output is correct |
10 |
Correct |
23 ms |
6480 KB |
Output is correct |
11 |
Correct |
14 ms |
6480 KB |
Output is correct |
12 |
Incorrect |
14 ms |
6480 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |