429B - Working out - CodeForces Solution


dp *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll   long long
#define fast ios_base::sync_with_stdio(0); cin.tie(0);  cout.tie(0);
#define el '\n'
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 1e3 + 7;
const int MOD = 1e9 + 7;

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};



// think very carefully before writing a line of code //
// enjoy the problems //
// never focus on performance during a contest //

int dp[N][N][4];
int A[N][N];
int n, m;


void doWork() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> A[i][j];
        }
    }
    // coming from 1,1 and moving left and down
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j][0] = A[i][j] + max(dp[i - 1][j][0], dp[i][j - 1][0]);
        }
    }
    // coming from n, 1 and moving up and right
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= m; j++) {
            dp[i][j][1] = A[i][j] + max(dp[i + 1][j][1], dp[i][j - 1][1]);
        }
    }
    // coming from n, m and moving up and left
    for (int i = n; i >= 1; i--) {
        for (int j = m; j >= 1; j--) {
            dp[i][j][2] = A[i][j] + max(dp[i + 1][j][2], dp[i][j + 1][2]);
        }
    }
    // coming from 1, m and moving left and down
    for (int i = 1; i <= n; i++) {
        for (int j = m; j >= 1; j--) {
            dp[i][j][3] = A[i][j] + max(dp[i - 1][j][3], dp[i][j + 1][3]);
        }
    }

    int ans = 0;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i - 1 >= 1 && i + 1 <= n && j - 1 >= 1 && j + 1 <= m) {
                ans = max(ans, dp[i - 1][j][0] + dp[i + 1][j][2] + dp[i][j + 1][3] + dp[i][j - 1][1]);
                ans = max(ans, dp[i - 1][j][3] + dp[i + 1][j][1] + dp[i][j + 1][2] + dp[i][j - 1][0]);
            }
        }
    }
    cout << ans;

}

signed main() {
    fast
//    freopen("snakes.in", "r", stdin);
//    freopen("snakes.out", "w", stdout);
    int testcases = 1;
//    cin >> testcases;
    for (int i = 1; i <= testcases; i++) {
        doWork();
    }
}
		  	 	  		  			 	 	  		 		 	


Comments

Submit
0 Comments
More Questions

2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem