#include <bits/stdc++.h>
#define maxn 100010
#define maxk 110
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
typedef long long ll;
using namespace std;
const char *fi="BLOCKS.INP";
const char *fo="BLOCKS.OUT";
int n,k,A[maxn],P[maxn];
ll F[maxn][maxk];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen(fi,"r",stdin);
// freopen(fo,"w",stdout);
cin>>n>>k;
FOR(i,1,n) cin>>A[i];
P[0]=0;
FOR(i,1,n) P[i]=max(P[i-1],A[i]);
FOR(i,1,n) F[i][1]=P[i];
FOR(j,2,k){
int maxright=0; ll sumleft=F[j-1][j-1];
FOR(i,j,n){
maxright = max(maxright,A[i]);
// cout<<i<<' '<<j<<' '<<sumleft<<' '<<maxright<<' '<<(F[i-1][j-1]+P[i-1]+A[i])<<endl;
if (sumleft+maxright < F[i-1][j-1]+A[i]) F[i][j]=sumleft+maxright;
else {
sumleft = F[i-1][j-1];
maxright = A[i];
F[i][j]=F[i-1][j-1]+A[i];
}
}
}
cout<<F[n][k];
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:18:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(fi,"r",stdin);
~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |