제출 #118706

#제출 시각아이디문제언어결과실행 시간메모리
118706sofhiasouzaMaja (COCI18_maja)C++14
44 / 110
373 ms784 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const int maxn = 110, maxs = 1e4+10; int n, m, a, b, mat[maxn][maxn], k; struct pt { ll f; int sf, ss; bool operator > (const pt &k) const{ if(f != k.f) return f > k.f; else if(sf != k.sf) return sf > k.sf; else return ss >= k.ss; } }dp[maxn][maxn][2]; pt solve() { int mod = 0; for(int t = 1 ; t <= min(k, 10000) ; t++) { mod ^= 1; for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= m ; j++) { if(dp[i-1][j][mod^1] > dp[i+1][j][mod^1] and dp[i-1][j][mod^1] > dp[i][j-1][mod^1] and dp[i-1][j][mod^1] > dp[i][j+1][mod^1]) { dp[i][j][mod] = dp[i-1][j][mod^1]; if(mat[i][j] > dp[i-1][j][mod^1].sf) { dp[i][j][mod].ss = dp[i-1][j][mod^1].sf; dp[i][j][mod].sf = mat[i][j]; } else if(mat[i][j] > dp[i-1][j][mod^1].ss) dp[i][j][mod].ss = mat[i][j]; } else if(dp[i+1][j][mod^1] > dp[i-1][j][mod^1] and dp[i+1][j][mod^1] > dp[i][j-1][mod^1] and dp[i+1][j][mod^1] > dp[i][j+1][mod^1]) { dp[i][j][mod] = dp[i+1][j][mod^1]; if(mat[i][j] > dp[i+1][j][mod^1].sf) { dp[i][j][mod].ss = dp[i+1][j][mod^1].sf; dp[i][j][mod].sf = mat[i][j]; } else if(mat[i][j] > dp[i+1][j][mod^1].ss) dp[i][j][mod].ss = mat[i][j]; } else if(dp[i][j-1][mod^1] > dp[i-1][j][mod^1] and dp[i][j-1][mod^1] > dp[i+1][j][mod^1] and dp[i][j-1][mod^1] > dp[i][j+1][mod^1]) { dp[i][j][mod] = dp[i][j-1][mod^1]; if(mat[i][j] > dp[i][j-1][mod^1].sf) { dp[i][j][mod].ss = dp[i][j-1][mod^1].sf; dp[i][j][mod].sf = mat[i][j]; } else if(mat[i][j] > dp[i][j-1][mod^1].ss) dp[i][j][mod].ss = mat[i][j]; } else { dp[i][j][mod] = dp[i][j+1][mod^1]; if(mat[i][j] > dp[i][j+1][mod^1].sf) { dp[i][j][mod].ss = dp[i][j+1][mod^1].sf; dp[i][j][mod].sf = mat[i][j]; } else if(mat[i][j] > dp[i][j+1][mod^1].ss) dp[i][j][mod].ss = mat[i][j]; } if(dp[i][j][mod].f != -1) dp[i][j][mod].f += mat[i][j]; if(dp[i][j][mod].sf < dp[i][j][mod].ss) swap(dp[i][j][mod].sf, dp[i][j][mod].ss); } } } return dp[a][b][mod]; } int main() { cin >> n >> m >> a >> b >> k; for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= m ; j++) { cin >> mat[i][j]; } } memset(dp, -1, sizeof dp); dp[a][b][0].f = 0; pt resp = solve(); if(k < 10000) cout << resp.f << endl; else cout << (ll)resp.f + (ll)(resp.sf+resp.ss)*((ll)k-10000)/2 << endl; }
#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...