This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define left 2*i+1
#define righ 2*i+2
#define mid (l+r)/2
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll maxn=2222;
const ll inf=1e12+10;
const ll mod=1e9+7;
const ll base=27;
ll n,k;
ll dp[maxn][maxn][11];
ll dpr[maxn][maxn][11];
ll dpc[maxn][maxn][11];
ll a[maxn][maxn];
ll sum;
int main()
{
IOS
cin>>n>>k;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=n;j++){
cin>>a[i][j];
sum+=a[i][j];
}
}
for(ll i=0;i<=n;i++){
for(ll j=0;j<=n;j++){
for(ll d=0;d<=k;d++){
dp[i][j][d]=-inf;
dpr[i][j][d]=-inf;
dpc[i][j][d]=-inf;
}
}
}
for(ll r=1;r<=n;r++){
dpr[r][0][0]=0;
for(ll c=1;c<=n;c++){
for(ll d=0;d<=k;d++){
dpr[r][c][d]=dpr[r][c-1][d];
if(c-2>=0&&d)dpr[r][c][d]=max(dpr[r][c][d],a[r][c]+a[r][c-1]+dpr[r][c-2][d-1]);
}
}
}
/*for(int d=0;d<=k;d++){
cout<<"******"<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<dpr[i][j][d]<<' ';
}
cout<<endl;
}
}*/
for(ll c=1;c<=n;c++){
dpc[0][c][0]=0;
for(ll r=1;r<=n;r++){
for(ll d=0;d<=k;d++){
dpc[r][c][d]=dpc[r-1][c][d];
if(r-2>=0&&d)dpc[r][c][d]=max(dpc[r][c][d],a[r][c]+a[r-1][c]+dpc[r-2][c][d-1]);
}
}
}
for(ll i=0;i<=n;i++){
dp[0][i][0]=dp[i][0][0]=0;
}
for(ll r=1;r<=n;r++){
for(ll c=1;c<=n;c++){
for(ll d=0;d<=k;d++){
for(ll d1=0;d1<=d;d1++){
ll d2=d-d1;
dp[r][c][d]=max(dp[r][c][d],dpr[r][c][d1]+dp[r-1][c][d2]);
dp[r][c][d]=max(dp[r][c][d],dpc[r][c][d1]+dp[r][c-1][d2]);
}
}
}
}
cout<<sum-dp[n][n][k]<<endl;
return 0;
}
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |