Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, m;
- int mat[55][55];
- int dp[55][55];
- int max_sum(int i, int j) {
- if(i == n - 1 and j == m - 1) {
- return mat[i][j];
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int result = 0;
- if(i + 1 < n) {
- result = max(result, max_sum(i + 1, j) + mat[i][j]);
- }
- if(j + 1 < m) {
- result = max(result, max_sum(i, j + 1) + mat[i][j]);
- }
- return dp[i][j] = result;
- }
- int main() {
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- dp[i][j] = -1;
- }
- }
- cout << max_sum(0, 0) << endl;
- return 0;
- }
- // f(0, 0) = f(1, 0) f(0, 1)
- // f(1, 0) = f(2, 0) f(1, 1)
- // f(0, 1) = f(1, 1) f(2, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement