Problem:
枠
画像の中から有益な情報を抽出する画像認識はコンピュータサイエンスの中でも重要な研究テーマのひとつです。デジタルカメラ、運転支援システム、防犯システムなどに幅広く応用されています。
このような研究のおかげで、私たちは画像解析を行うための多くのソフトウェアやプログラム集を使い様々な処理を行うことができます。一方、自力でプログラムを書いて解析することで、その仕組みを知り、楽しい時間を過ごすことができます。ここでは、一風変わった画像認識をしてみましょう。
画像として次のような各ピクセルが整数の値を持つ
N
×
N
のピクセルが入力として与えられます。この画像の中から、線の太さが１ピクセルの長方形の枠（わく）を１つ抽出します。
枠が覆うピクセルの値の和が最大となるような枠を抽出して、その和を報告するプログラムを作成して
下さい。ただし、下の図のように、縦、横のピクセル数が１つや２つの場合も枠とみなすものとします。
入力
入力は以下の形式で与えられる。
N
p
1,1
p
1,2
...
p
1,N
p
2,1
p
2,2
...
p
2,N
:
p
N,1
p
N,2
...
p
N,N
１行目に縦と横のピクセル数
N
(1 ≤
N
≤ 300) が与えられる。続く
N
行に、
i
行
j
列目のピクセルの値を表す整数
p
i,j
(-1000 ≤
p
i,j
≤ 1000)が与えられる。
出力
ピクセル値の和が最大となるような枠の、ピクセル値の和を１行に出力する。
入出力例
入力例１
5
2 0 0 2 0
0 1 0 2 0
0 0 0 -1 0
0 4 0 3 0 
-1 0 0 1 0
出力例１
12
入力例２
3
0 0 0
0 -1 0
0 0 0
出力例２
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int N;
	cin >> N;
	vector P(N, vector<ll>(N + 1));
	rep(i, 0, N) rep(j, 1, N + 1) cin >> P[i][j];
	rep(i, 0, N) rep(j, 1, N + 1) P[i][j] += P[i][j - 1];
	
	if (N == 1) {
		cout << P[0][1] << '\n';
		return 0;
	}
	
	ll ans = -(1ll << 60);
	rep(l, 1, N + 1) rep(r, l, N + 1) {
		ll val = -(1ll << 60);
		
		rep(i, 0, N) {
			if (i >= 2) {
				if (l == r) val += P[i - 1][r] - P[i - 1][l - 1];
				else val += P[i - 1][r] - P[i - 1][r - 1] + P[i - 1][l] - P[i - 1][l - 1];
			}
			if (i >= 1)	val = max(val, P[i - 1][r] - P[i - 1][l - 1]);
			ans = max(ans, val + P[i][r] - P[i][l - 1]);
		}
	}
	cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 3860
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
int n, a[309][309], sa[309][309], s[309], t[309], st[309], r[309];
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a[i][j];
			sa[i][j + 1] = sa[i][j] + a[i][j];
		}
	}
	int ret = -inf;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j <= n; j++) {
			for (int k = 0; k < n; k++) {
				s[k] = sa[k][j] - sa[k][i];
				t[k] = (j - i >= 2 ? a[k][i] + a[k][j - 1] : a[k][i]);
				st[k + 1] = st[k] + t[k];
				r[k] = st[k] + s[k];
			}
			int p = -inf;
			for (int k = 0; k < n; k++) {
				ret = max(ret, r[k] + p);
				ret = max(ret, s[k]);
				p = max(p, s[k] - st[k + 1]);
			}
		}
	}
	cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4084
final_rank: 9

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0310.cc: 枠
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 300;
const int INF = 1 << 30;

/* typedef */

/* global variables */

int ps[MAX_N][MAX_N];
int hss[MAX_N + 1][MAX_N + 1], vss[MAX_N + 1][MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) scanf("%d", ps[i] + j);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) hss[i][j + 1] = hss[i][j] + ps[i][j];
  for (int j = 0; j < n; j++)
    for (int i = 0; i < n; i++) vss[i + 1][j] = vss[i][j] + ps[i][j];

  int maxs = -INF;
  for (int i0 = 0; i0 < n; i0++) {
    for (int j = 0, mx = 0; j < n; j++) {
      mx = max(mx, -hss[i0][j]);
      maxs = max(maxs, hss[i0][j + 1] + mx);
    }

    for (int i1 = i0 + 1; i1 < n; i1++) {
      for (int j = 0; j < n; j++)
	maxs = max(maxs, vss[i1 + 1][j] - vss[i0][j]);
      for (int j = 1, mx = 0; j < n; j++) {
	mx = max(mx,
		 vss[i1][j - 1] - vss[i0 + 1][j - 1]
		 - hss[i0][j - 1] - hss[i1][j - 1]);
	maxs = max(maxs,
		   vss[i1][j] - vss[i0 + 1][j]
		   + hss[i0][j + 1] + hss[i1][j + 1] + mx);
      }
    }
  }

  printf("%d\n", maxs);
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4020
final_rank: 1

code_snippet: #include<stdio.h>

long long max( long long a, long long b )
{
    return a > b ? a : b;
}

int main( void )
{
    int N;
    scanf( "%d", &N );

    int pixel[301][301], sum[301][301];
    for( int j = 0; j <= N; j++ )
    {
        sum[0][j] = 0;
    }
    long long ans = -1e15;
    for( int i = 0; i < N; i++ )
    {
        for( int j = 0; j < N; j++ )
        {
            scanf( "%d", &pixel[i][j] );
            sum[i+1][j] = sum[i][j] + pixel[i][j];
            ans = max( ans, pixel[i][j] );
        }
    }

    for( int i = 0; i < N; i++ )
    {
        for( int left = 0; left < N; left++ )
        {
            long long tmp_sum1 = 0, tmp_sum2 = 0;
            for( int right = left; right < N; right++ )
            {
                tmp_sum1 = max( tmp_sum1, 0 ) + pixel[i][right];
                if( i + 1 < N ) tmp_sum2 += pixel[i+1][right];
                ans = max( ans, max( tmp_sum1, tmp_sum1 + tmp_sum2 ) );
            }
        }
    }

    for( int j = 0; j < N; j++ )
    {
        for( int up = 0; up < N; up++ )
        {
            long long tmp_sum1 = 0, tmp_sum2 = 0;
            for( int down = up; down < N; down++ )
            {
                tmp_sum1 = max( tmp_sum1, 0 ) + pixel[down][j];
                if( j + 1 < N ) tmp_sum2 += pixel[down][j+1];
                ans = max( ans, max( tmp_sum1, tmp_sum1 + tmp_sum2 ) );
            }
        }
    }

    for( int up = 0; up < N; up++ )
    {
        for( int down = up + 2; down < N; down++ )
        {
            long long tmp = sum[down+1][N-1] - sum[up][N-1];
            for( int left = N - 3; left >= 0; left-- )
            {
                tmp = max( tmp, sum[down+1][left+2] - sum[up][left+2] );
                tmp += pixel[up][left+1] + pixel[down][left+1];
                ans = max( ans, tmp + ( sum[down+1][left] - sum[up][left] ) );
            }
        }
    }

    printf( "%lld\n", ans );

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3676
final_rank: 7

code_snippet: #include<stdio.h>

long long max( long long a, long long b )
{
    return a > b ? a : b;
}

int main( void )
{
    int N;
    scanf( "%d", &N );

    int pixel[301][301], sum[301][301];
    for( int j = 0; j <= N; j++ )
    {
        sum[0][j] = 0;
    }
    long long ans = -1e15;
    for( int i = 0; i < N; i++ )
    {
        for( int j = 0; j < N; j++ )
        {
            scanf( "%d", &pixel[i][j] );
            sum[i+1][j] = sum[i][j] + pixel[i][j];
            ans = max( ans, pixel[i][j] );
        }
    }

    for( int i = 0; i < N; i++ )
    {
        for( int left = 0; left < N; left++ )
        {
            long long tmp_sum1 = 0, tmp_sum2 = 0;
            for( int right = left; right < N; right++ )
            {
                tmp_sum1 += pixel[i][right];
                if( i + 1 < N ) tmp_sum2 += pixel[i+1][right];
                ans = max( ans, max( tmp_sum1, tmp_sum1 + tmp_sum2 ) );
            }
        }
    }

    for( int j = 0; j < N; j++ )
    {
        for( int up = 0; up < N; up++ )
        {
            long long tmp_sum1 = 0, tmp_sum2 = 0;
            for( int down = up; down < N; down++ )
            {
                tmp_sum1 += pixel[down][j];
                if( j + 1 < N ) tmp_sum2 += pixel[down][j+1];
                ans = max( ans, max( tmp_sum1, tmp_sum1 + tmp_sum2 ) );
            }
        }
    }

    for( int up = 0; up < N; up++ )
    {
        for( int down = up + 2; down < N; down++ )
        {
            long long tmp = sum[down+1][N-1] - sum[up][N-1];
            for( int left = N - 3; left >= 0; left-- )
            {
                tmp = max( tmp, sum[down+1][left+2] - sum[up][left+2] );
                tmp += pixel[up][left+1] + pixel[down][left+1];
                ans = max( ans, tmp + ( sum[down+1][left] - sum[up][left] ) );
            }
        }
    }

    printf( "%lld\n", ans );

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3680
final_rank: 8

code_snippet: #include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
long long int map[303][303];
long long int ysums[303];
//遅いしかっこ悪いけど愚直で行くか。
//これはかなり遅いコードなので多分コード実行速度は下から数えた方が早いし、正答者一覧でも同じ秒数の人はいないだろうね
//簡単な発想なのでコードの解説は簡単にできると思おう、賢い子なら小学生でもわかる。

int main() {
	int n;

	cin>>n;
	for(int i=0;i<n;i++){
		long long int s2;
		for(int j=0;j<n;j++){
			cin>>map[i][j];
		}
	}
	long long int ans=map[0][0];
	for(int y=0;y<n;y++){
		for(int x=0;x<n;x++){
			long long int ysum=0,ty;
			for(int y2=y;y2>=0;y2--){
				if(x==0){
					ysum+=map[y2][x];
					ysums[y2]=ysum;
					ans=max(ysum,ans);
				}else{
					ysum+=map[y2][x];
					ans=max(ans,ysum);
					if(y==y2)continue;
					ans=max(ans,ysum+ysums[y2]);
					ty=ysums[y2]+map[y2][x]+map[y][x];
					if(ty<ysum){
						ysums[y2]=ysum;
					}else{
						ysums[y2]=ty;
					}
				}
			}
			//for(int y2=0;y2<=y;y2++){
			//	cout<<ysums[y2]<<" ";
			//}
			//cout<<endl;
			long long int sumx=0;
			for(int xl=x;xl>=0;xl--){
				sumx+=map[y][xl];
				ans=max(ans,sumx);
			}
		}
	}
	cout<<ans<<endl;
	
}
accuracy: 1
time_ms: 60
memory_kb: 3824
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

//イテレーション
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto& i : I)
// x:コンテナ
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647           // 2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807  // 9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007             //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x)                                                   \
    for (auto i = x.begin(); i != --x.end(); i++) cout << *i << " "; \
    cout << *--x.end() << endl;

// aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a, ll b) { return (a + (b - 1)) / b; }
ll myfloor(ll a, ll b) { return a / b; }

int main() {
    //小数の桁数の出力指定
    // cout<<fixed<<setprecision(10);
    //入力の高速化用のコード
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    ll N;
    cin >> N;

    vector<vector<int>> ps(N+1, vector<int>(N+1, 0));
    REP(i, N) REP(j, N) {
        cin >> ps[1+i][1+j];
    }

    vector<vector<int>> vs(N+1, vector<int>(N+1, 0));
    vector<vector<int>> hs(N+1, vector<int>(N+1, 0));

    FOR(i, 1, N) FOR(j, 1, N) {
        vs[i][j] = vs[i-1][j] + ps[i][j];
        hs[i][j] = hs[i][j-1] + ps[i][j];
    }

    /*
    FOR(b, 1, N) FOR(r, 1, N) {
        cout << sums[b][r] << " ";
        if(r == N) cout << endl;
    }
    */
    vector<vector<vector<int>>> ms(N+1, vector<vector<int>>(N+1, vector<int>(N, 0)));
    FOR(b, 1, N) FOR(t, 1, b) {
        if(b == t) {
            FOR(l, 1, N-1) {
                ms[b][t][l] = max(ms[b][t][l-1], -hs[t][l]);
            }
        } else {
            ms[b][t][1] = vs[b][1] - vs[t-1][1] - hs[t][1] - hs[b][1];
            FOR(l, 2, N-1) {
                ms[b][t][l] = max(ms[b][t][l-1], vs[b][l] - vs[t-1][l] - hs[t][l] - hs[b][l]);
            }
        }
    }
    /*
    FOR(b, 1, N) FOR(r, 1, N) {
        cout << vs[b][r] << " ";
        if(r == N) cout << endl;
    }
    */

    ll ans = ps[1][1];

    FOR(b, 1, N) FOR(r, 1, N) {
        FOR(t, 1, b) {
            ll tmp;
            if(b == t) {
                tmp = hs[t][r] + ms[b][t][r-1];
            }else {
                tmp = vs[b][r] - vs[t-1][r] + max(hs[b][r-1] + hs[t][r-1] + ms[b][t][r-1], 0);
            }
            if(ans < tmp) {
                ans = tmp;
            }
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 114232
final_rank: 19

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;

  BIT(int n){
    N = n;
    node.resize(N+10);
  }

  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash {
    static const int base1 = 1007, base2 = 2009;
    static const int mod1 = 1000000007, mod2 = 1000000009;
    vector<long long> hash1, hash2, power1, power2;
    int n;
    // construct
    RollingHash(const string &S) {
        n = (int)S.size();
        hash1.assign(n+1, 0);
        hash2.assign(n+1, 0);
        power1.assign(n+1, 1);
        power2.assign(n+1, 1);
        for (int i = 0; i < n; ++i) {
            hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
            hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
            power1[i+1] = (power1[i] * base1) % mod1;
            power2[i+1] = (power2[i] * base2) % mod2;
        }
    }
    
    // get hash of S[left:right]
    inline pair<long long, long long> get(int l, int r) const {
        long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
        if (res1 < 0) res1 += mod1;
        long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
        if (res2 < 0) res2 += mod2;
        return {res1, res2};
    }
 
    inline pair<long long, long long> c_shift(int l) {
      auto h1 = get(0, l);
      auto h2 = get(l, n);
      return {(h1.first + h2.first * power1[l]) % mod1, (h1.second + h2.second * power2[l]) % mod2};
    }
 
    // get lcp of S[a:] and T[b:]
    inline int getLCP(int a, int b) const {
        int len = min((int)hash1.size()-a, (int)hash1.size()-b);
        int low = 0, high = len;
        while (high - low > 1) {
            int mid = (low + high) >> 1;
            if (get(a, a+mid) != get(b, b+mid)) high = mid;
            else low = mid;
        }
        return low;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int n; cin >> n;
  vector<vector<int>> p(n, vector<int>(n));
  int ans = -1e9;
  REP(i,n) REP(j,n) cin >> p[i][j];
  
  vector<vector<int>> cums(n, vector<int>(n+1));
  REP(i,n) {
    REP(j,n) {
      cums[i][j+1] = cums[i][j] + p[i][j];
    }
  }
  for(int i=0; i<n; i++) {
    for(int j=i; j<n; j++) {
      vector<vector<int>> dp(n, vector<int>(4, -1e9));
      REP(k,n) {
        dp[k][1] = max(dp[k][1], cums[k][j+1]-cums[k][i]);
        dp[k][3] = max(dp[k][3], cums[k][j+1]-cums[k][i]);
        if(k-1>=0){
          int tmp = p[k][i];
          if(i != j) tmp += p[k][j];
          dp[k][2] = max({dp[k][2], dp[k-1][2] + tmp, dp[k-1][1] + tmp});
          dp[k][3] = max({dp[k][3], dp[k-1][1] + cums[k][j+1]-cums[k][i], dp[k-1][2] + cums[k][j+1]-cums[k][i]});
        }
      }
      REP(k,n) ans = max(ans, dp[k][3]);
    }
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3876
final_rank: 18

code_snippet: #include <stdio.h>
#include <iostream>

int table[302][302] = { {} };

int max(int x, int y)
{
	return x > y ? x : y;
}

int sum(int y1, int x1, int y2, int x2)
{
	return (table[y2][x2] - table[y2][x1 - 1] - table[y1-1][x2] + table[y1 - 1][x1 - 1]);
}

int main() 
{
	int n;
	std::cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			std::cin >> table[i][j];
			table[i][j] += table[i - 1][j] + table[i][j - 1] - table[i-1][j-1];
		}
	}

	int ans = -1000;
	int best = 0;
	for (int row1 = 1; row1 <= n; row1++)
	{
		for (int row2 = row1; row2 <= n; row2++)
		{
			best = 0;
			for (int k = 1; k <= n; k++)
			{
				int t1 = sum(row1, k, row2, k);
				int t2 = sum(row1, k, row1, k);
				if (row1 != row2) t2 += sum(row2, k, row2, k);
				ans = max(ans, best+t1);
				ans = max(ans, t1);
				best = max(t1, best + t2);
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3508
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int p[305][305], S[305][305], suffix[305], sum[305];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            cin >> p[i][j];
            S[i][j + 1] = S[i][j] + p[i][j];
        }
    }

    int ans = -INF;
    suffix[N] = -INF;
    for(int l = 0; l < N; ++l){
        for(int i = 0; i < N; ++i){
            sum[i + 1] = sum[i] + p[i][l];
        }
        for(int i = N - 1; i >= 0; --i){
            suffix[i] = max(suffix[i + 1], sum[i + 1]);
        }
        for(int i = 0; i < N; ++i){
            chmax(ans, max(suffix[i + 1] - sum[i], p[i][l]));
        }
    }
    for(int l = 0; l < N; ++l){
        for(int r = l + 2; r <= N; ++r){
            for(int i = 0; i < N; ++i){
                sum[i + 1] = sum[i] + p[i][l] + p[i][r - 1];
            }
            for(int i = N - 1; i >= 0; --i){
                suffix[i] = max(suffix[i + 1], sum[i + 1] + S[i][r - 1] - S[i][l + 1]);
            }
            for(int i = 0; i < N; ++i){
                chmax(ans, max(suffix[i + 1] - sum[i] + S[i][r - 1] - S[i][l + 1], S[i][r] - S[i][l]));
            }
        }
    }

    cout << ans << '\n';

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4100
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N;
int p[300][300];
int Xs[301][300],Ys[300][301];
int ans=-1e9;
main()
{
	cin>>N;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)
	{
		cin>>p[i][j];
		Xs[i+1][j]=Xs[i][j]+p[i][j];
		Ys[i][j+1]=Ys[i][j]+p[i][j];
	}
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=j+1;k<=N;k++)
	{
		ans=max(ans,max(Xs[k][i]-Xs[j][i],Ys[i][k]-Ys[i][j]));
	}
	for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)
	{
		int mx=-1e9;
		for(int k=1;k<N;k++)
		{
			mx=max(mx,Xs[j][k-1]-Xs[i+1][k-1]-Ys[i][k-1]-Ys[j][k-1]);
			ans=max(ans,mx+Ys[i][k]+Ys[j][k]+Xs[j+1][k]-Xs[i][k]);
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4132
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n;
    cin >> n;
    auto v = make_v(n, n, 0), S = make_v(n+1, n+1, 0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> v[i][j];
            S[i+1][j+1] = v[i][j];
            S[i+1][j+1] += S[i][j+1] + S[i+1][j] - S[i][j];
        }
    }
    int ans = -INF<int>;
    auto f = [&](int y1, int x1, int y2, int x2){
        return S[y2][x2]-S[y2][x1]-S[y1][x2]+S[y1][x1];
    };
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j <= n; ++j) {
            if(j-i == 1){
                int val = 0, mn = 0;
                for (int k = 0; k < n; ++k) {
                    val += v[i][k];
                    chmax(ans, val-mn);
                    chmin(mn, val);
                }
                continue;
            }
            for (int k = 0; k < n; ++k) {
                chmax(ans, f(i, k, j, k+1));
            }
            int mx = f(i, 0, j, 1);
            for (int k = 1; k < n; ++k) {
                chmax(ans, mx+f(i, k, j, k+1));
                mx += v[i][k]+v[j-1][k];
                chmax(mx, f(i, k, j, k+1));
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3668
final_rank: 6

code_snippet: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n,p[333][333];
int tate[333][333];
int main()
{
    cin >> n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) cin >> p[i][j];
    }
    
    memset(tate,0,sizeof(int));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            tate[i][j] = p[i][j]+tate[i-1][j];
        }
    }
    
    int ans = p[1][1];
    for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
            int retu = tate[j][1]-tate[i-1][1];
            for(int k=2;k<=n;k++){
                ans = max(ans,retu+tate[j][k]-tate[i-1][k]);
                if(i == j) retu = max(retu+p[i][k],tate[j][k]-tate[i-1][k]);
                else retu = max(retu+p[i][k]+p[j][k],tate[j][k]-tate[i-1][k]);
            }
        }
    }
    
    cout << ans << endl;
    
    return(0);
}
accuracy: 1
time_ms: 30
memory_kb: 3872
final_rank: 4

code_snippet: /*
　　　∫ ∫ ∫
　　　ノヽ
　　（＿　 ）
　（＿　　　 ）
（＿＿＿＿＿＿ ）
　ヽ(´･ω･)ﾉ　
　　 |　 /
　　 UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
using P = pair<int64, int64>;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
     os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

struct cumsum2D{
    int H,W;
    vector<vector<int64>> cumsum;
    cumsum2D(vector<vector<int64>> A) :H(A.size()), W(A[0].size()){
        cumsum.assign(H+1, vector<int64>(W+1,0));
        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                cumsum[h+1][w+1] = A[h][w];
            }
        }

        for(int h=0;h<H;h++){
            for(int w=0;w<W;w++){
                cumsum[h+1][w+1] += cumsum[h+1][w] + cumsum[h][w+1] - cumsum[h][w];
            }
        }
    }

    // return sum(A[h][w]) {h,w | h in [h_begin, h_end) and w in [w_begin, w_end)}
    int64 get(int h_begin, int h_end, int w_begin, int w_end){
        return cumsum[h_end][w_end] - cumsum[h_begin][w_end] - cumsum[h_end][w_begin] + cumsum[h_begin][w_begin];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<vector<int64>> P(N,vector<int64>(N));
    REP(i,N){
        REP(j,N) cin >> P[i][j];
    }

    int64 ans = -INF;
    cumsum2D cumsum(P);
    REP(w,N){
        REP(u,N){
            for(int d=u+1; d<=N;d++){
                chmax(ans, cumsum.get(u,d,w,w+1));
                int h=w,l=u,r=d;
                chmax(ans, cumsum.get(h,h+1,l,r));
            }
        }
    }
    REP(u,N){
        for(int d=u+1; d<N; d++){
            int64 S = cumsum.get(u,u+1,0,N) + cumsum.get(d,d+1,0,N);
            vector<int64> DP_L(N,-INF), DP_R(N,-INF);
            REP(l,N){
                if(l) DP_L[l] = DP_L[l-1];
                int64 val = - (cumsum.get(u,u+1,0,l) + cumsum.get(d,d+1,0,l)) + (cumsum.get(u+1,d,l,l+1) ) ;
                chmax(DP_L[l], val);

                int r = N-l-1;
                if(l) DP_R[r] = DP_R[r+1];
                val =  - (cumsum.get(u,u+1,r+1,N) + cumsum.get(d,d+1,r+1,N)) + (cumsum.get(u+1,d,r,r+1) ) ;
                chmax(DP_R[r], val);
            }
            REP(i,N-1) chmax(ans, S + DP_L[i] + DP_R[i+1]);
        }
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 5008
final_rank: 16

code_snippet: // そすうささんのブログを読んだ
#include<iostream>
using namespace std;

int a[302][302], yoko[302][302] = {}, tate[302][302] = {};

int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
            yoko[i][j] = a[i][j]+yoko[i][j-1];
            tate[i][j] = a[i][j]+tate[i-1][j];
        }
    }
    int ans = -1000;
    // line
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            for(int k = 1; k <= n; k++){
                ans = max(ans, yoko[k][j]-yoko[k][i-1]);
                ans = max(ans, tate[j][k]-tate[i-1][k]);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            int all = yoko[i][n]+yoko[j][n];
            int submax = (tate[j-1][1]-tate[i][1]) - yoko[j][0] - yoko[i][0];
            for(int k = 2; k <= n; k++){
                int right = tate[j-1][k]-tate[i][k] - (yoko[j][n]-yoko[j][k]) - (yoko[i][n]-yoko[i][k]);
                ans = max(ans, all+submax+right);
                // cout << i << " "<< j << " " << k << " " << all << " " << submax << " " << right << endl;
                submax = max(submax, (tate[j-1][k]-tate[i][k])-yoko[j][k-1]-yoko[i][k-1]);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4160
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int c_sum_tate[300][301];
int c_sum_yoko[300][301];
int c_sum_2[300][300][301];

int main() {
    int n;
    cin >> n;
    vector<vector<int>> p(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> p[i][j];
        }
    }

    // c_sum_tate[x] = 左からx列目の累積和
    for (int i = 0; i < 300; i++)
        for (int j = 0; j <= 300; j++)c_sum_tate[i][j] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            c_sum_tate[i][j + 1] = c_sum_tate[i][j] + p[j][i];
        }
    }
    // c_sum_yoko[x] = 上からx行目の累積和
    for (int i = 0; i < 300; i++)
        for (int j = 0; j <= 300; j++)c_sum_yoko[i][j] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            c_sum_yoko[i][j + 1] = c_sum_yoko[i][j] + p[i][j];
        }
    }

    // コ形 の累積和を持つ
    // c_sum2[x][y][z]=上からx行目(0-indexed)と上からy行目(0-indexed)のz番目(1-indexed)までカウント
    for (int i = 0; i < 300; i++)
        for (int j = 0; j < 300; j++)
            for (int k = 0; k <= 300; k++)c_sum_2[i][j][k] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            for (int k = 1; k <= n; k++) {
                c_sum_2[i][j][k] = c_sum_yoko[i][k - 1];
                if (j != i)c_sum_2[i][j][k] += c_sum_yoko[j][k - 1];

                c_sum_2[i][j][k] += c_sum_tate[k - 1][j + 1] - c_sum_tate[k - 1][i];
            }

            // c_sum_2の累積Maxを右から取る
            for (int k = n - 1; k >= 0; k--) c_sum_2[i][j][k] = max(c_sum_2[i][j][k], c_sum_2[i][j][k + 1]);
        }
    }

    int ret = -1145141919;
    for (int sx = 0; sx < n; sx++) {
        for (int sy = 0; sy < n; sy++) {
            int lside = 0;
            for (int ex = sx; ex < n; ex++) {
                lside += p[ex][sy];
                ret = max(ret, lside);
                if (sy < n - 1) {
                    int box = lside + c_sum_2[sx][ex][sy + 2] - c_sum_yoko[sx][sy + 1];
                    if (ex != sx)box -= c_sum_yoko[ex][sy + 1];

                    ret = max(ret, box);
                }
            }
        }
    }

    cout << ret << endl;
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 109804
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

int p[500][500];
int sum[500][500];

int main(){
	int n;cin>>n;
	rep(i,n)rep(j,n)scanf("%d",&p[i][j]);
	rep(i,n){
		rep(j,n){
			sum[i+1][j]=sum[i][j]+p[i][j];
		}
	}
	int ans=INT_MIN;
	rep(i,n){
		int Max=0;
		rep(k,n){
			ans=max(ans,Max+p[i][k]);
			Max=max(Max+p[i][k],0);
		}
		for(int j=i+1;j<n;j++){
			int Max=0;
			rep(k,n){
				ans=max(ans,max(0,Max)+(sum[j+1][k]-sum[i][k]));
				Max=max(Max+p[i][k]+p[j][k],sum[j+1][k]-sum[i][k]);
			}
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4368
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
ll pic[305][305]={0};
ll sum[305][305]={0};
ll rsum(int x1,int y1,int x2,int y2){
  if(x1>=x2||y1>=y2) return 0;
  ll ret=sum[x2][y2]+sum[x1][y1];
  return ret-sum[x2][y1]-sum[x1][y2];
}
int main(){
  int n;
  cin>>n;
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++) cin>>pic[i][j];
  }

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++) sum[i][j]=sum[i][j-1]+pic[i][j];
  }
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++) sum[i][j]+=sum[i-1][j];
  }

  ll ans=-17441744;
  for(int i=1;i<=n;i++){
    for(int j=i;j<=n;j++){
      //l=i r=j

      vector<ll> vec;
      vec.push_back(0);
      vec.push_back(rsum(i-1,0,j,1));
      for(int k=2;k<=n;k++){
        vec.push_back(vec[k-1]+rsum(i-1,k-1,j,k)-rsum(i,k-2,j-1,k-1));
      }

      priority_queue<int> pque;
      pque.push(vec[n]);
      ll kans=pque.top();
      for(int k=n-1;k>=1;k--){
        kans=max(kans,pque.top()-vec[k]+rsum(i,k-1,j-1,k)+rsum(i-1,k-1,j,k));
        //if(i==1&&j==5) cout<<pque.top()-vec[k]+rsum(i,k-1,j-1,k)+rsum(i-1,k-1,j,k)<<endl;
        pque.push(vec[k]);
      }
      /*if(i==1&&j==5){
        for(int k=1;k<=n;k++) cout<<vec[k]<<" ";
        cout<<endl;
      }*/

      ans=max(ans,kans);
      //if(i==4&&j==4) cout<<i<<" "<<j<<" "<<kans<<endl;

      //if(kans==14) cout<<i<<"!"<<j<<endl;
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 170
memory_kb: 4608
final_rank: 17

code_snippet: #include <bits/stdc++.h>

#define int long long
using namespace std;

int n;
int num[400][400];
int rui[400][400];

int solve(int l, int r){
	int dp[400];
	dp[n-1] = INT_MIN;
	
	if(l == r){
		for(int i = n-2;i >= 0;i--){
		 	dp[i] = max(num[i+1][l], dp[i+1] + num[i+1][l]);
		 }
	}else{
		for(int i = n-2;i >= 0;i--){
			dp[i] = max(rui[i+1][r+1]-rui[i+1][l], dp[i+1] + num[i+1][r] + num[i+1][l]);
		}
	}
	
	int ans = INT_MIN;
	for(int i = 0;i < n;i++){
		int tmp = rui[i][r+1]-rui[i][l];
		ans = max({ans, tmp, tmp + dp[i]});
	}
	
	return ans;
}

signed main(){
	cin >> n;
	
	for(int i = 0;i < n;i++){
		for(int j = 0;j < n;j++){
			cin >> num[i][j];
			rui[i][j+1] += rui[i][j] + num[i][j];
		}
	}
	
	int ans = INT_MIN;
	for(int i = 0;i < n;i++){
		for(int j = i;j < n;j++){
			ans = max(ans, solve(i, j));
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4964
final_rank: 15

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;

    vector< vector<int> > p(n, vector<int>(n));
    for(int i = 0 ; i < n ; ++i){
        for(int j = 0 ; j < n ; ++j){
            cin >> p[i][j];
        }
    }

    vector< vector<int> > sum(n + 1, vector<int>(n + 1, 0));
    for(int i = 0 ; i < n ; ++i){
        for(int j = 0 ; j < n ; ++j){
            sum[i + 1][j + 1] = p[i][j];
        }
    }

    for(int i = 0 ; i <= n ; ++i){
        for(int j = 1 ; j <= n ; ++j){
            sum[i][j] += sum[i][j - 1];
        }
    }

    for(int j = 0 ; j <= n ; ++j){
        for(int i = 1 ; i <= n ; ++i){
            sum[i][j] += sum[i - 1][j];
        }
    }

    int ans = -(1 << 30);

    for(int i = 1 ; i <= n ; ++i){
        for(int j = 0 ; j <= n ; ++j){
            for(int k = 0 ; k < j ; ++k){
                ans = max(ans, sum[i][j] - sum[i][k] - sum[i - 1][j] + sum[i - 1][k]);
                ans = max(ans, sum[j][i] - sum[k][i] - sum[j][i - 1] + sum[k][i - 1]);
            }
        }
    }

    for(int d = 2 ; d <= n ; ++d){
        for(int u = 1 ; u < d ; ++u){
            int maxi = sum[d][1] - sum[d][0] - sum[u - 1][1] + sum[u - 1][0];
            for(int j = 2 ; j <= n ; ++j){
                ans = max(ans, maxi + sum[d][j] - sum[d][j - 1] - sum[u - 1][j] + sum[u - 1][j - 1]);
                maxi = max(maxi + p[d - 1][j - 1] + p[u - 1][j - 1], sum[d][j] - sum[d][j - 1] - sum[u - 1][j] + sum[u - 1][j - 1]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3648
final_rank: 13