제출 #221370

#제출 시각아이디문제언어결과실행 시간메모리
221370qkxwsmMaxcomp (info1cup18_maxcomp)C++14
100 / 100
163 ms17144 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 1013;

int N, M;
int grid[MAXN][MAXN];
int ans;
int stor[MAXN][MAXN];

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> M;
    FOR(i, 0, N)
    {
        FOR(j, 0, M)
        {
            cin >> grid[i][j];
        }
    }
    //x-x0 + y-y0 + grid[x][y] - grid[x0][y0]
    FOR(i, 0, N)
    {
        FOR(j, 0, M)
        {
            stor[i][j] = -grid[i][j] + i + j;
        }
    }
    FOR(i, 0, N)
    {
        FOR(j, 0, M)
        {
            if (i) ckmax(stor[i][j], stor[i - 1][j]);
            if (j) ckmax(stor[i][j], stor[i][j - 1]);
            ckmax(ans, -i - j + grid[i][j] + stor[i][j]);
        }
    }
    // cerr << "ANS " << ans << endl;
    //x-x0 + y-y0 - grid[x][y] + grid[x0][y0]
    FOR(i, 0, N)
    {
        FOR(j, 0, M)
        {
            stor[i][j] = grid[i][j] + i + j;
        }
    }
    FOR(i, 0, N)
    {
        FOR(j, 0, M)
        {
            if (i) ckmax(stor[i][j], stor[i - 1][j]);
            if (j) ckmax(stor[i][j], stor[i][j - 1]);
            ckmax(ans, -i - j - grid[i][j] + stor[i][j]);
        }
    }
    // cerr << "ANS " << ans << endl;
    //x-x0 + y0-y + grid[x][y0] - grid[x0][y]
    FOR(i, 0, N)
    {
        FORD(j, M, 0)
        {
            stor[i][j] = grid[i][j] + i - j;
        }
    }
    FOR(i, 0, N)
    {
        FORD(j, M, 0)
        {
            if (i) ckmax(stor[i][j], stor[i - 1][j]);
            if (j != M - 1) ckmax(stor[i][j], stor[i][j + 1]);
            ckmax(ans, -i + j - grid[i][j] + stor[i][j]);
        }
    }
    // cerr << "ANS " << ans << endl;
    FOR(i, 0, N)
    {
        FORD(j, M, 0)
        {
            stor[i][j] = -grid[i][j] + i - j;
        }
    }
    FOR(i, 0, N)
    {
        FORD(j, M, 0)
        {
            if (i) ckmax(stor[i][j], stor[i - 1][j]);
            if (j != M - 1) ckmax(stor[i][j], stor[i][j + 1]);
            ckmax(ans, -i + j + grid[i][j] + stor[i][j]);
        }
    }
    // cerr << "ANS " << ans << endl;
    cout << ans - 1 << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...