Submission #229214

#TimeUsernameProblemLanguageResultExecution timeMemory
229214VEGAnnMaja (COCI18_maja)C++14
110 / 110
473 ms640 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("fast-math") //#pragma GCC optimize("no-stack-protector") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define PB push_back #define all(x) x.begin(),x.end() #define pii pair<int,int> #define MP make_pair #define ft first #define sd second #define sz(x) ((int)x.size()) using namespace std; using namespace __gnu_pbds; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; const int N = 110; const int PW = 22; const int oo = 2e9; int steps[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; int c[N][N], a, b, n, m, k, ite, it; ll ans[2][N][N], res = 0; bool yeee_boy; void MAX(ll &x, ll y){ x = max(x, y); } void check(){ if (yeee_boy){ if (ite == k) { cout << ans[it][a][b] - 1; exit(0); } } else { if (k < ite + ite) return; ll kol = (k - ite - ite) / 2ll; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++){ if (ans[it][i][j] == 0) continue; ll cur = ans[it][i][j] - 1; for (int z = 0; z < 4; z++){ int cx = steps[z][0] + i, cy = steps[z][1] + j; if (cx < 0 || cy < 0 || cx >= n || cy >= m) continue; res = max(res, cur * 2 + kol * ((ll)c[i][j] + c[cx][cy]) - c[i][j]); } } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); #ifdef _LOCAL freopen("in.txt","r",stdin); #endif // _LOCAL cin >> n >> m >> a >> b >> k; a--; b--; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> c[i][j]; yeee_boy = bool(k <= m * n); ite = 0; it = 0; ans[0][a][b] = 1; check(); for (ite = 1; ite <= min(n * m, k); ite++){ for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans[it ^ 1][i][j] = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++){ if (ans[it][i][j] == 0) continue; if (i > 0) MAX(ans[it ^ 1][i - 1][j], ans[it][i][j] + c[i - 1][j]); if (j > 0) MAX(ans[it ^ 1][i][j - 1], ans[it][i][j] + c[i][j - 1]); if (j + 1 < m) MAX(ans[it ^ 1][i][j + 1], ans[it][i][j] + c[i][j + 1]); if (i + 1 < n) MAX(ans[it ^ 1][i + 1][j], ans[it][i][j] + c[i + 1][j]); } it ^= 1; check(); } cout << res; 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...