이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(x) (x).begin(), (x).end()
int n, k;
vector<ll> a = {};
void s(){
vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(k+1, vector<ll>(2, -1e15)));
ll ans=0;
dp[0][0][1]=0; dp[0][0][0]=0;
for(int i=1; i<=n; i++){
dp[i][0][0]=0;
for(int j=1; j<=k; j++){
dp[i][j][0]=max(dp[i-1][j][1], dp[i-1][j][0]);
dp[i][j][1]=max(dp[i-1][j][1], dp[i-1][j-1][0])+a[i-1];
// cout << i << ' ' << j << ' ' << dp[i][j][0] << ' ' << dp[i][j][1] << endl;
ans=max({ans,dp[i][j][0],dp[i][j][1]});
}
}
cout << ans << endl;
}
void f() {
ll ans = 0, nw = 0;
for (auto& u : a) {
nw = max(0LL, nw + u);
ans = max(ans, nw);
}
cout << ans << endl;
return;
}
int main() {
cin >> n >> k; int cntn=0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
a.pb(x);
if(x<0) cntn++;
}
if (k == 1) {
f();
return 0;
}
if(cntn<=1){
ll cnt = 0;
for (auto& u : a)
if (u > 0) cnt += u;
cout << cnt << endl;
}
s();
}
# | 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... |