#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define repn(i, a, b) for(ll i=ll(b)-1; i>=ll(a); i--)
#define ff first
#define ss second
#define si size()
#define pb push_back
#define be begin()
#define en end()
const ll N=100010;
const ll K=110;
const ll inf=(1e9);
vector<vector<ll>> dp(N, vector<ll> (K, -2));
vector<vector<ll>> mx(N, vector<ll> (N));
vector<ll> pre(N);
vector<ll> a(N);
ll sol(ll n, ll k){
if(n<k) return dp[n][k]=-1;
if(k==1) return dp[n][k]=mx[1][n];
if(k==n) return dp[n][k]=pre[n];
if(dp[n][k]!=-2){
return dp[n][k];
}
rep(i, 0, n){
if(dp[i][k-1]==-2) dp[i][k-1]=sol(i, k-1);
if(dp[i][k-1]!=-1){
if(dp[n][k]==-2) dp[n][k]=dp[i][k-1]+mx[i+1][n];
else dp[n][k]=min(dp[n][k], dp[i][k-1]+mx[i+1][n]);
}
}
if(dp[n][k]==-2) return dp[n][k]=-1;
return dp[n][k];
}
void solve(){
ll n, k;
cin>>n>>k;
rep(i, 1, n+1){
cin>>a[i];
}
rep(i, 1, n+1){
mx[i][i]=a[i];
rep(j, i+1, n+1){
mx[i][j]=max(a[j], mx[i][j-1]);
}
}
rep(i, 1, n+1){
pre[i]=pre[i-1]+a[i];
}
cout<<sol(n, k);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t=1;
//~ cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
125 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
132 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
125 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
125 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |