Submission #1222936

#TimeUsernameProblemLanguageResultExecution timeMemory
1222936VMaksimoski008Maxcomp (info1cup18_maxcomp)C++20
100 / 100
276 ms16092 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;

int n, m, a[N][N], dp1[N][N], dp2[N][N];

void rotate() {
    int b[m+1][n+1];
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++) b[j][n-i+1] = a[i][j];
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++) a[i][j] = b[i][j];
    swap(n, m);
}

int solve() {
    int ans = -1;
    for(int i=0; i<=n; i++)
        for(int j=0; j<=m; j++) dp1[i][j] = dp2[i][j] = -2e9;
    
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            dp1[i][j] = max({ dp1[i-1][j], dp1[i][j-1], -a[i][j] + i + j });
            dp2[i][j] = max({ dp2[i-1][j], dp2[i][j-1], a[i][j] + i + j });

            ans = max(ans, dp1[i][j] + a[i][j] - i - j - 1);
            ans = max(ans, dp2[i][j] - a[i][j] - i - j - 1);
        }
    }
    
    return ans;
}

int main() {
    cin >> n >> m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++) cin >> a[i][j];

    int ans = -1;
    for(int i=0; i<4; i++) {
        ans = max(ans, solve());
        rotate();
    }

    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...