This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
long long best(long long a, long long b, long long c, long long d) {
return max(max(a, b), max(c, d));
}
int main() {
long long n, m, a, b, k;
cin >> n >> m >> a >> b >> k;
a--;
b--;
long long grid[105][105];
long long dp[105][105];
bool vis[105][105];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
dp[i][j] = -1e9;
vis[i][j] = false;
}
}
dp[a][b] = 0;
vis[a][b] = true;
queue<pair<int, int>> check;
check.push({ a + 1, b });
check.push({ a - 1, b });
check.push({ a, b - 1 });
check.push({ a, b + 1 });
while (check.size() > 0) {
int x = check.front().first, y = check.front().second;
check.pop();
vis[x][y] = true;
if (x < 0 || x >= n || y < 0 || y >= m || dp[x][y] >= 0) {
continue;
}
dp[x][y] = best(dp[x - 1][y], dp[x + 1][y], dp[x][y - 1], dp[x][y + 1]) + grid[x][y];
if(x + 1 < n && !vis[x + 1][y])
check.push({ x + 1, y });
if(x - 1 >= 0 && !vis[x - 1][y])
check.push({ x - 1, y });
if(y + 1 < m && !vis[x][y + 1])
check.push({ x, y + 1 });
if(y - 1 >= 0 && !vis[x][y - 1])
check.push({ x, y - 1 });
}
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long long diff = max(i - a, a - i) + max(j - b, b - j);
long long cur = best(grid[i - 1][j], grid[i + 1][j], grid[i][j - 1], grid[i][j + 1]) + grid[i][j];
ans = max(ans, dp[i][j] + dp[i][j] - grid[i][j] + (k - diff * 2) / 2 * cur);
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |