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 <string.h>
#define INF -987654321
using namespace std;
int R, C;
int arr[303][303];
int cache[303][303];
int psum_element(int y, int x, int val) {
int ret = val;
if (x - 1 >= 1) {
ret += arr[y][x-1];
}
if (y - 1 >= 1) {
ret += arr[y-1][x];
}
if (x - 1 >= 1 && y - 1>= 1)
ret -= arr[y-1][x-1];
return ret;
}
int dp (int y, int x) {
if (y == 0 || x == 0)
return INF;
if (y == 1)
return arr[1][x];
if (x == 1)
return arr[y][1];
int &ret = cache[y][x];
ret = INF;
for (int i = y - 1; i >= 0; i--) {
for (int j = x - 1; j >= 0; j--) {
if (cache[i][j] != -1)
ret = max(ret, cache[i][j] + arr[y][x] - arr[y][j] - arr[i][x] + arr[i][j]);
else
ret = max(ret, dp(i, j) + arr[y][x] - arr[y][j] - arr[i][x] + arr[i][j]);
}
}
ret = max(ret, arr[y][x]);
return ret;
}
int main() {
cin >> R >> C;
memset(cache, -1, sizeof(cache));
int temp;
for (int i = 1 ; i <= R; i++) {
for (int j = 1 ; j <= C; j++) {
cin >> temp;
arr[i][j] = psum_element(i, j, temp);
}
} // psum 2차원 배열을 완성
cout << dp(R, C) << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |