fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define nmax (long long)(1e6+7)
  5. #define oo (long long)(1e18)
  6. #define INF (int)(1e9+7)
  7. #define fi first
  8. #define se second
  9. #define pii pair<int, int>
  10. #define Hung ""
  11. using namespace std;
  12.  
  13. ll m, n, x, y, t, a[500][500], dp[500][500][50];
  14. ll dx[] = {0, 1, 0, -1};
  15. ll dy[] = {1, 0, -1, 0};
  16.  
  17. int main()
  18. {
  19. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  20. if (fopen(Hung".inp", "r")){
  21. freopen(Hung".inp", "r", stdin);
  22. freopen(Hung".out", "w", stdout);
  23. }
  24. cin >> m >> n >> x >> y >> t;
  25. for (ll i = 1; i <= m; i++)
  26. for (ll j = 1; j <= n; j++)
  27. cin >> a[i][j];
  28.  
  29. for (ll i = 0; i <= m; i++)
  30. for (ll j = 0; j <= n; j++)
  31. for (ll k = 0; k <= t; k++)
  32. dp[i][j][k] = -oo;
  33. dp[x][y][0] = 0;
  34.  
  35. for (ll k = 0; k < t; k++)
  36. for (ll i = 1; i <= m; i++)
  37. for (ll j = 1; j <= n; j++)
  38. for (ll q = 0; q <= 3; q++){
  39. ll x = i + dx[q];
  40. ll y = j + dy[q];
  41. dp[x][y][k + 1] = max(dp[x][y][k + 1], dp[i][j][k] + a[x][y]);
  42. }
  43. ll res = -oo;
  44. for (ll i = 1; i <= m; i++)
  45. for (ll j = 1; j <= n; j++)
  46. res = max(res, dp[i][j][t]);
  47. cout << res;
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
-1000000000000000000