Submission #86101

#TimeUsernameProblemLanguageResultExecution timeMemory
86101ShtefMaja (COCI18_maja)C++14
66 / 110
2056 ms1356 KiB
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; int n, m, x1, y1, k, odkud[5][105][105]; ll dp[5][105][105], c[105][105], sol; const ll inf = 1e18; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; int main () { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> x1 >> y1 >> k; for(int i = 1 ; i <= n ; ++i){ for(int j = 1 ; j <= m ; ++j){ cin >> c[i][j]; } } for(int i = 1 ; i <= n ; ++i){ for(int j = 1 ; j <= m ; ++j){ for(int v = 0 ; v < 5 ; ++v){ dp[v][i][j] = -inf; } } } for(int i = 0 ; i < 5 ; ++i){ dp[i][x1][y1] = 0; } memset(odkud, -1, sizeof(odkud)); for(int i = 1 ; i <= min(k, 3 * n * m) ; ++i){ for(int j = 1 ; j <= n ; ++j){ for(int v = 1 ; v <= m ; ++v){ for(int w = 0 ; w < 4 ; ++w){ int nx = j + dx[w]; int ny = v + dy[w]; if(nx <= 0 || nx > n || ny <= 0 || ny > m) continue; if(dp[i % 5][nx][ny] < dp[(i - 1 + 5) % 5][j][v] + c[nx][ny]){ odkud[i % 5][nx][ny] = w; dp[i % 5][nx][ny] = dp[(i - 1 + 5) % 5][j][v] + c[nx][ny]; } } } } for(int j = 1 ; j <= n ; ++j){ for(int v = 1 ; v <= m ; ++v){ if(odkud[i % 5][j][v] == odkud[(i - 2 + 5) % 5][j][v]){ int j1 = j - dx[odkud[i % 5][j][v]], v1 = v - dy[odkud[i % 5][j][v]]; sol = max(sol, 2 * dp[(i - 2 + 5) % 5][j][v] + ((c[j][v] + c[j1][v1]) * ((k - (i - 2) * 2) / 2)) - c[j][v]); } } } } cout << (k <= 3 * n * m ? dp[k & 5][x1][y1] : sol) << endl; return 0; }
#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...