Submission #89412

#TimeUsernameProblemLanguageResultExecution timeMemory
89412kraljlavova1Maja (COCI18_maja)C++11
110 / 110
475 ms1368 KiB
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int,int> pii; const int MAXN=110; int n,m,a,b,k; ll P[MAXN][MAXN]; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; ll dp[MAXN][MAXN][2],best[MAXN][MAXN]; bool valid(int x,int y){ if(x<0||x>=n) return false; if(y<0||y>=m) return false; return true; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); cin>>n>>m>>a>>b>>k; a--;b--; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>P[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ dp[i][j][0]=-1; dp[i][j][1]=-1; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ best[i][j]=-4e18; for(int d=0;d<4;d++){ if(valid(i+dx[d],j+dy[d])){ best[i][j]=max(best[i][j],P[i][j]+P[i+dx[d]][j+dy[d]]); } } } } dp[a][b][0]++; int kk=n*m; for(int c=0;c<min(k/2,n*m);c++){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(dp[i][j][0]!=-1){ int x,y; for(int d=0;d<4;d++){ x=i+dx[d]; y=j+dy[d]; if(valid(x,y)){ dp[x][y][1]=max(dp[x][y][1],dp[i][j][0]+P[x][y]); } } } } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ dp[i][j][0]=dp[i][j][1]; dp[i][j][1]=-1; } } } if(k/2<=n*m){ ll sol=-4e18; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ sol=max(sol,2*dp[i][j][0]-P[i][j]); } } cout<<sol<<"\n"; } else{ ll sol=-4e18,x=(k-2*n*m)/2; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ sol=max(sol,2*dp[i][j][0]+x*best[i][j]-P[i][j]); } } cout<<sol<<"\n"; } return 0; }

Compilation message (stderr)

maja.cpp: In function 'int main()':
maja.cpp:43:6: warning: unused variable 'kk' [-Wunused-variable]
  int kk=n*m;
      ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...