Problem:
Maximum Sum Sequence II
与えられた整数の行列
a
1,1
a
1,2
...
a
1,n
a
2,1
a
2,2
...
a
2,n
:
a
n,1
a
n,2
...
a
n, n
で、縦・横方向に１つ以上連続した項（部分行列）の和の最大値を出力して終了するプログラムを作成して下さい。
Input
入力データは以下の形式で与えられます。
n
a
1,1
a
1,2
...
a
1,n
a
2,1
a
2,2
...
a
2,n
:
a
n,1
a
n,2
...
a
n, n
n
は 1 以上 100 以下、
a
i,j
は -10000 以上 10000 以下です。
Output
最大値を 1 行に出力して下さい。
Sample Input 1
3
1 -2 3
-4 5 6
7 8 -9
Output for the Sample Input 1
16
この入力の場合、以下の部分行列の項の和が最大となります。
-4 5
7 8
Sample Input 2
4
1 3 -9 2
2 7 -1 5
-8 3 2 -1
5 0 -3 1
Output for the Sample Input 2
15
この入力の場合、以下の部分行列の項の和が最大となります。
7 -1 5
3 2 -1

Submitted solutions:

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll n;
  cin >> n;
  vv(ll, a, n+1, n+1, 0);
  rep(i, 1, n+1) rep(j, 1, n+1) cin >> a[i][j];
  rep(i, 1, n+1) rep(j, 2, n+1) a[i][j] += a[i][j-1];
  rep(i, 2, n+1) rep(j, 1, n+1) a[i][j] += a[i-1][j];
  ll ans = -inf;
  rep(sy, 1, n+1) rep(sx, 1, n+1) {
    rep(ey, sy, n+1) rep(ex, sx, n+1) {
      chmax(ans, a[ey][ex] - a[sy-1][ex] - a[ey][sx-1] + a[sy-1][sx-1]);
    }
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3520
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
int main() {
    ll N;
    cin>>N;
    vvll A(N+1,vll(N+1,0));
    rep(i,N)rep(j,N)cin>>A[i+1][j+1];
    rep(i,N)rep(j,N+1)A[i+1][j]+=A[i][j];
    rep(i,N+1)rep(j,N)A[i][j+1]+=A[i][j];
    ll an=-1e18;
    rep(i,N+1)rep(j,i){
        rep(p,N+1)rep(q,p){
            ll d=A[i][p]+A[j][q]-A[i][q]-A[j][p];
            an=max(an,d);
        }
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3520
final_rank: 14

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

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

	auto N=in();
	vvll M(N,vll(N)),S(N+1,vll(N+1,0));
	REP(i,N){
		REP(j,N){
			M[i][j]=in();
		}
	}
	REP(i,N){
		REP(j,N){
			S[i+1][j+1] = S[i+1][j]+M[i][j];
		}
	}
	REP(j,N+1){
		REP(i,N){
			S[i+1][j] += S[i][j];
		}
	}

	ll A=-(1ll<<50);
	for(ll h=1;h<=N;h++){
		for(ll w=1;w<=N;w++){
			for(ll v=0;v<=N-h;v++){
				for(ll u=0;u<=N-w;u++){
					chmax(A,S[v+h][u+w]+S[v][u]-S[v+h][u]-S[v][u+w]);
				}
			}
		}
	}
	out(A);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3560
final_rank: 16

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class t> using vc = vector<t>;
template<class t> using vvc = vc<vc<t>>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vc<int>;
using vvi = vvc<int>;
using vl = vc<ll>;
using vvl = vvc<ll>;

#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define irep(i,a,b) for (int i = (a); i > (b); i--)
#define all(a) a.begin(),a.end()
#define print(n) cout << n << '\n'
#define pritn(n) print(n)
#define printv(n,a) {copy(all(n),ostream_iterator<a>(cout," ")); cout<<"\n";}
#define printvv(n,a) {for(auto itr:n) printv(itr,a);}
#define rup(a,b) (a+b-1)/b
#define input(A,N) rep(i,0,N) cin>>A[i]
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)


int main(){
    cout << fixed << setprecision(15);
    
    int n;
    cin>>n;
    vvi a(n,vi(n));
    rep(i,0,n){
        rep(j,0,n){
            cin>>a[i][j];
        }
    }
    vvi sum(n+1,vi(n+1,0));
    int now = 0;
    rep(i,0,n)rep(j,0,n)sum[i+1][j+1]=a[i][j];
    rep(i,0,n)rep(j,0,n+1)sum[i+1][j]+=sum[i][j];
    rep(i,0,n+1)rep(j,0,n)sum[i][j+1]+=sum[i][j];
    //printvv(sum,int);

    int ans = -(int)1e9;
    
    for(int i = 1;i<n+1;i++){
        for(int j = 1;j<n+1;j++){
            for(int k = 0;k<i;k++){
                for(int l = 0;l<j;l++){
                    chmax (ans,sum[i][j]-sum[i][l]-sum[k][j]+sum[k][l]);
                }
            }
        }
    }
    print(ans);
    
    //system("pause");
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 13

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>

#define N 100

using namespace std;

int memo[N][N];

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

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(i == 0 && j == 0) {
                memo[i][j] = in[i][j];
            } else if(i == 0) {
                memo[i][j] = memo[i][j - 1] + in[i][j];
            } else if(j == 0) {
                memo[i][j] = memo[i - 1][j] + in[i][j];
            } else {
                memo[i][j] = memo[i - 1][j] + memo[i][j - 1] - memo[i - 1][j - 1] + in[i][j];
            }
        }
    }

    int ans = -10000;
    for(int si = 0; si < n; si++) {
        for(int sj = 0; sj < n; sj++) {
            for(int i = si; i < n; i++) {
                for(int j = sj; j < n; j++) {
                    int n = memo[i][j];
                    if(si > 0) {
                        n -= memo[si - 1][j];
                    }

                    if(sj > 0) {
                        n -= memo[i][sj - 1];
                    }
                    if(si > 0 && sj > 0){
                        n += memo[si - 1][sj - 1];
                    }

                    ans = max(ans, n);
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3164
final_rank: 3

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)

int main(){
    int n; cin >> n;
    vector<vector<int> > A(n,vector<int>(n));
    rep(i,n)
        rep(j,n) cin >> A[i][j];

    vector<vector<long long> > accumA(n+1,vector<long long>(n+1,0));
    
    rep(i,n)
        rep(j,n)
            accumA[i+1][j+1] = accumA[i+1][j] + accumA[i][j+1] - accumA[i][j] + A[i][j];


    long long ans = -1000000;
    rep(i,n+1)
        rep(j,n+1)
            rep(k,i)
                rep(l,j)
                    ans = max(ans, accumA[i][j] - accumA[i][l] - accumA[k][j] + accumA[k][l]);

    cout << ans << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 3252
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

//2次元累積和
template< class T > struct CumulativeSum2D{
    vector<vector<T>> data;
    CumulativeSum2D(int H, int W) : data(H + 1, vector<T>(W + 1, 0)) {}
    void add(int y, int x, int z){
        y++, x++;
        if(y>=data.size()||x>=data[0].size())return;
        data[y][x]+=z;
    }
    void build(){
        for(int i=1;i<data.size();i++) {
            for(int j=1;j<data[i].size();j++) {
                data[i][j]+=data[i][j-1]+data[i-1][j]-data[i-1][j-1];
            }
        }
    }
    T query(int sy,int sx,int gy, int gx){
        return (data[gy][gx]-data[sy][gx]-data[gy][sx]+data[sy][sx]);
    }
};

int main(){
    INT(n);
    CumulativeSum2D<ll> S(n,n);
    int v;
    rep(y,n){
        rep(x,n){
            in(v);
            S.add(y,x,v);
        }
    }
    S.build();
    ll ans=-INF2;
    for(int lx=0;lx<n;lx++){
        for(int rx=lx+1;rx<=n;rx++){
            for(int ly=0;ly<n;ly++){
                for(int ry=ly+1;ry<=n;ry++){
                    chmax(ans,S.query(ly,lx,ry,rx));
                }
            }
        }
    }
    out(ans);
}
accuracy: 1
time_ms: 20
memory_kb: 3656
final_rank: 19

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

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

  int n;
  cin >> n;

  vector<vector<int>> a(n + 1, vector<int>(n + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a.at(i).at(j);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a.at(i).at(j) += a.at(i).at(j - 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a.at(i).at(j) += a.at(i - 1).at(j);
    }
  }

  int ans = -2e9;
  for (int i1 = 1; i1 <= n; i1++) {
    for (int j1 = 1; j1 <= n; j1++) {
      for (int i2 = 0; i2 < i1; i2++) {
        for (int j2 = 0; j2 < j1; j2++) {
          ans = max(ans, a[i1][j1] - a[i1][j2] - a[i2][j1] + a[i2][j2]);
        }
      }
    }
  }

  cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 12

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int inf=mod*mod;
const double eps=1e-9;
signed main(){
	int n,a[100][100],sum[101][101]={};
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
			sum[i+1][j+1]=sum[i+1][j]+sum[i][j+1]-sum[i][j]+a[i][j];
		}
	}
	int ans=-inf;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			for(int k=i+1;k<=n;k++)
				for(int l=j+1;l<=n;l++)
					ans=max(ans,sum[k][l]-sum[k][j]-sum[i][l]+sum[i][j]);
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3596
final_rank: 17

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 main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    int n; cin >> n;
    ll ans = -1e18;
    vector<vector<ll>> a(n, vector<ll> (n));
    rep(i,n)rep(j,n) cin >> a[i][j];
    vector<vector<ll>> s(n + 1, vector<ll>(n + 1, 0));
    rep(i,n)rep(j,n) s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + a[i][j];
    rep(i,n+1)rep(j,n+1)rep(k,n+1)rep(l,n+1)if(i <= k && j <= l && i && j){
        ans = max(ans, s[k][l] - s[i-1][l] - s[k][j-1] + s[i-1][j-1]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 3256
final_rank: 20

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

using namespace std;
using ll = long long;
using pint = pair<int,int>;

template<class T> class CumulativeSum2D { private:
	int H, W;
	vector<vector<T>> G;
public:
	CumulativeSum2D(){};
	CumulativeSum2D(const vector<vector<T>>& g) {
		H = g.size();
		W = g[0].size();
		G.resize(H + 1, vector<T>(W + 1));
		for(int i = 0; i < H; i++) {
			for(int j = 0; j < W; j++) {
				G[i + 1][j + 1] = G[i + 1][j] + G[i][j + 1] - G[i][j] + g[i][j];
			}
		}
	}

	//[(y1, x1) ~ (y2, x2))
	T get(int y1, int x1, int y2, int x2) {
		return G[y2][x2] - G[y1][x2] - G[y2][x1] + G[y1][x1];
	}
};

int main()
{
	int N; cin >> N;
	vector<vector<int>> g(N, vector<int>(N));
	for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) cin >> g[i][j];
	CumulativeSum2D<int> cum(g);

	int ans = cum.get(0, 0, N, N);
	for(int i = 0; i <= N; i++) {
		for(int j = i + 1; j <= N; j++) {
			for(int k = 0; k <= N; k++) {
				for(int l = k + 1; l <= N; l++) {
					ans = max(ans, cum.get(i, k, j, l));
				}
			}
		}
	}

	cout << ans << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 3456
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i< (n); i++)
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int inf = (1<<30);
int main(){
	int n;
	cin>>n;
	int a[n][n]={};
	rep(i,n){
		rep(j,n){
			cin>>a[i][j];
		}
	}
	int dp[n+1][n+1]={};
	for(int i=1; i<=n; i++) dp[1][i]=dp[1][i-1]+a[0][i-1];
	
	for(int i=1; i<=n; i++) dp[i][1]=dp[i-1][1]+a[i-1][0];
	
	for(int i=2; i<=n; i++){
		for(int j=2; j<=n; j++){
			dp[i][j]=dp[i-1][j]+dp[i][j-1]+a[i-1][j-1]-dp[i-1][j-1];
		}
	}
	/*
	rep(i,n+1){
		rep(j,n+1){
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	*/
	int t=0;
	int ans=-inf;
	for(int h=1; h<=n; h++){
		for(int i=1; i<=n; i++){
			for(int j=0; j<=h-1; j++){
				for(int k=0; k<=i-1; k++){
					t=dp[h][i]+dp[j][k]-dp[j][i]-dp[h][k];
					ans=max(ans,t);
				}
			}
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3176
final_rank: 1

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

using namespace std;
//using namespace atcoder;
using ll = long long;
using Graph = vector<vector<int>>;

#define rep(i,m,n) for(int (i)=(m);(i)<(n);++(i))
#define rrep(i,m,n) for(int (i)=(n)-1;(i)>=(m);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)

constexpr ll INF = (ll)1e10;
constexpr ll mod = (ll)1e9 + 7;
constexpr double PI = 3.1415926535897932;

template<class T>inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T>inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
inline void init() {
    cin.tie(nullptr); cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
}



//二次元累積和
struct AccSum2D {
    vector<vector<ll>>v;
    int H, W;
    AccSum2D(vector<vector<ll>>& vec) {
        H = vec.size();
        W = vec[0].size();
        v.resize(H + 1, vector<ll>(W + 1));
        rep(i, 0, H) {
            rep(j, 0, W) {
                v[i + 1][j + 1] = vec[i][j];
            }
        }

        //横に累積和
        rep(i, 0, H)rep(j, 0, W)v[i + 1][j + 1] += v[i + 1][j];
        //縦に累積和
        rep(i, 0, H)rep(j, 0, W)v[j + 1][i + 1] += v[j][i + 1];

    }
    //0-indexed
    ll sum(ll y1, ll x1, ll y2, ll x2) {
        ++y1; ++x1; ++y2; ++x2;
        return v[y2][x2] - v[y1 - 1][x2] - v[y2][x1 - 1] + v[y1 - 1][x1 - 1];
    }
    
};

int main() {
    init();

    int n; cin >> n;
    vector<vector<ll>>vec(n, vector<ll>(n));
    rep(i, 0, n)rep(j, 0, n)cin >> vec[i][j];
    AccSum2D Ac(vec);
    ll ans = -INF;
    rep(y1, 0, n)rep(x1, 0, n)rep(y2, y1, n)rep(x2, x1, n)chmax(ans, Ac.sum(y1, x1, y2, x2));

    cout << ans << "\n";

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3624
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#ifdef _DEBUG
#define PRE_COMMAND             \
    std::cin.rdbuf(in.rdbuf()); \
    std::cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10, PI = acos(-1.0);
template <class T> auto MAX(T& seq) {
    return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T& seq) {
    return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T& seq) {
    T temp{0};
    auto& temp2 = temp[0];
    return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T& seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T& seq, S& sort_order) {
    sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T>& seq) {
    sort(seq.begin(), seq.end(), greater<T>());
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
    long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1;
    while (k) {
        if (k & 1) now = (now * n) % mod;
        n = (n * n) % mod;
        k >>= 1;
    }
    return now;
}
template <typename T> istream& operator>>(istream& is, vector<T>& vec) {
    for (T& x : vec) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    if (!v.size()) return os;
    typename vector<T>::const_iterator ii = v.begin();
    os << *ii++;
    for (; ii != v.end(); ++ii) os << " " << *ii;
    return os;
}
// 累積和を求める関数
// 入力は配列と、最初に0を入れるか
template <class T> vector<T> accumulate(vector<T>& v, bool zero_start = true) {
    int n = (int)v.size();
    T sum = 0;
    vector<T> ret(n + zero_start, 0);
    for (int i = 0; i < n; i++) {
        sum += v[i];
        ret[i + zero_start] = sum;
    }
    return ret;
}
template <class T>
vector<vector<T>> accumulate(vector<vector<T>>& v, bool zero_start = true) {
    int h = (int)v.size(), w = (int)v[0].size();
    T sum = 0;
    vector<vector<T>> ret(h + zero_start, vector<T>(w + zero_start, 0));
    for (int i = 0; i < h; i++) {
        T sum = 0;
        for (int j = 0; j < w; j++) {
            sum += v[i][j];
            ret[i + zero_start][j + zero_start] = sum;
        }
    }
    for (int j = zero_start; j < w + zero_start; j++) {
        T sum = 0;
        for (int i = +zero_start; i < h + zero_start; i++) {
            sum += ret[i][j];
            ret[i][j] = sum;
        }
    }
    return ret;
}
// [a, b)x[c, d)を求める
template <class T> T calc(vector<vector<T>>& v, int a, int b, int c, int d) {
    return v[a][b] + v[c][d] - v[a][d] - v[c][b];
}

int main() {
    PRE_COMMAND
    int n;
    cin >> n;
    vector<vector<int>> a(n, vector<int>(n));
    cin >> a;
    vector<vector<int>> b(n + 1, vector<int>(n + 1));
    b = accumulate(a);
    int ans = -100000000;
    REP(i, n) {
        REP(j, n) {
            FOR(k, i + 1, n + 1) {
                FOR(l, j + 1, n + 1) ans = max(ans, calc(b, i, j, k, l));
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3292
final_rank: 8

code_snippet: #include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(int i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
const int inf = 1 << 31 - 1;
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(void) {
    int n; cin >> n; vector<vector<int>> v(n+1,vector<int>(n+1,0));
    req(i, n) req(j,n){
        cin >> v[i][j];
    }rep(i, n+1) rep(j, n) v[i][j + 1] += v[i][j];
    rep(i, n) rep(j, n + 1) v[i + 1][j] += v[i][j];
    int sum = -inf;
    req(i, n) req(j, n) rep(k, i) rep(l, j) {
        chmax(sum, v[i][j] - v[i][l] - v[k][j] + v[k][l]);
    }cout << sum << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3216
final_rank: 2

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

int n;
int acc[110][110];
 
signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
    rep(i, n) rep(j, n) {
        int a; cin >> a;
        acc[i+1][j+1] = acc[i+1][j]+acc[i][j+1]-acc[i][j]+a;
    }
    int ans = -1000000;
    rep(i, n) rep(j, n) for (int k=i; k<n; k++) for (int l=j; l<n; l++) {
        ans = max(ans, acc[k+1][l+1]-acc[k+1][j]-acc[i][l+1]+acc[i][j]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3308
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int inf = 1001001001;
int main(){
    int n,i,j,k,l,ans = -inf;scanf("%d",&n);
    int rp[n+1][n+1];
    for(i=0;i<=n;i++) rp[0][i] = rp[i][0] = 0;
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++) scanf("%d",&rp[i][j]);
    }
    for(i=1;i<=n;i++){
        for(j=2;j<=n;j++) rp[i][j] += rp[i][j-1];
    }
    for(i=1;i<=n;i++){
        for(j=2;j<=n;j++) rp[j][i] += rp[j-1][i];
    }
    for(i=1;i<=n;i++){
        for(j=i;j<=n;j++){
            for(k=1;k<=n;k++){
                for(l=k;l<=n;l++) ans = max(ans,rp[j][l]-rp[j][k-1]-rp[i-1][l]+rp[i-1][k-1]);
            }
        }
    }
    printf("%d\n",ans);
}
accuracy: 1
time_ms: 20
memory_kb: 3256
final_rank: 5

code_snippet: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;		using ld = long double;
using VB = vector<bool>;	using VVB = vector<VB>;
using VI = vector<int>;		using VVI = vector<VI>;
using VL = vector<ll>;		using VVL = vector<VL>;
using VS = vector<string>;	using VD = vector<ld>;
using PII = pair<int, int>;	using VP = vector<PII>;
using PLL = pair<ll, ll>;	using VPL = vector<PLL>;
template<class T>using PQ = priority_queue<T>;
template<class T>using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = (int)1e9;
constexpr ll inf_ll = (ll)1e18, MOD = 1000000007;
constexpr ld PI = M_PI, EPS = 1e-12;

// --- input --- //
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Input {
	static int gc() {
		return getchar_unlocked();
	}
	template<class T>static void i(T& v) {
		cin >> v;
	}
	static void i(char& v) {
		while (isspace(v = gc()));
	}
	static void i(bool& v) {
		v = in<char>() != '0';
	}
	static void i(string& v) {
		v.clear(); char c; for (i(c); !isspace(c); c = gc())v += c;
	}
	static void i(int& v) {
		bool neg = false; v = 0; char c; i(c);
		if (c == '-') { neg = true; c = gc(); }
		for (; isdigit(c); c = gc())v = v * 10 + (c - '0');
		if (neg)v = -v;
	}
	static void i(long long& v) {
		bool neg = false; v = 0; char c; i(c);
		if (c == '-') { neg = true; c = gc(); }
		for (; isdigit(c); c = gc())v = v * 10 + (c - '0');
		if (neg)v = -v;
	}
	static void i(double& v) {
		double dp = 1; bool neg = false, adp = false; v = 0; char c; i(c);
		if (c == '-') { neg = true; c = gc(); }
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.')adp = true;
			else if (adp)v += (c - '0') * (dp *= 0.1);
			else v = v * 10 + (c - '0');
		}
		if (neg)v = -v;
	}
	static void i(long double& v) {
		long double dp = 1; bool neg = false, adp = false; v = 0; char c; i(c);
		if (c == '-') { neg = true; c = gc(); }
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.')adp = true;
			else if (adp)v += (c - '0') * (dp *= 0.1);
			else v = v * 10 + (c - '0');
		}
		if (neg)v = -v;
	}
	template<class T, class U>static void i(pair<T, U>& v) {
		i(v.first); i(v.second);
	}
	template<class T>static void i(vector<T>& v) {
		for (auto& e : v)i(e);
	}
	struct InputV {
		int n, m;
		InputV(int _n) :n(_n), m(0) {}
		InputV(const pair<int, int>& nm) :n(nm.first), m(nm.second) {}
		template<class T>operator vector<T>() {
			vector<T> v(n); i(v); return v;
		}
		template<class T>operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m)); i(v); return v;
		}
	};
public:
	static string get_line() {
		string v; char c;
		for (i(c); c != '\n' && c != '\0'; c = gc())v += c;
		return v;
	}
	template<class T>static T in() {
		T v; i(v); return v;
	}
	template<class T>operator T()const {
		return in<T>();
	}
	int operator--(int)const {
		return in<int>() - 1;
	}
	InputV operator[](int n)const {
		return InputV(n);
	}
	InputV operator[](const pair<int, int>& n)const {
		return InputV(n);
	}
	void operator()()const {}
	template<class H, class...T>void operator()(H&& h, T&& ...t)const {
		i(h); operator()(forward<T>(t)...);
	}
}in;
#define input(T) Input::in<T>()
#define INT input(int)
#define LL input(ll)
#define STR input(string)
#define inputs(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(ll, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)

// --- output --- //
struct BoolStr {
	const char* t, * f; BoolStr(const char* _t, const char* _f) :t(_t), f(_f) {}
}Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char* d, * l; DivStr(const char* _d, const char* _l) :d(_d), l(_l) {}
}spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", "");
class Output {
	BoolStr B{ Yes }; DivStr D{ spc };
	void p(int v)const {
		if (v < 0)putchar_unlocked('-'), v = -v;
		char b[10]; int i = 0;
		while (v)b[i++] = '0' + v % 10, v /= 10;
		if (!i)b[i++] = '0';
		while (i--)putchar_unlocked(b[i]);
	}
	void p(ll v)const {
		if (v < 0)putchar_unlocked('-'), v = -v;
		char b[20]; int i = 0;
		while (v)b[i++] = '0' + v % 10, v /= 10;
		if (!i)b[i++] = '0';
		while (i--)putchar_unlocked(b[i]);
	}
	void p(bool v)const {
		p(v ? B.t : B.f);
	}
	void p(char v)const {
		putchar_unlocked(v);
	}
	void p(const char* v)const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void p(double v)const {
		printf("%.20f", v);
	}
	void p(ld v)const {
		printf("%.20Lf", v);
	}
	template<class T> void p(const T& v)const {
		cout << v;
	}
	template<class T, class U>void p(const pair<T, U>& v)const {
		p(v.first); p(D.d); p(v.second);
	}
	template<class T>void p(const vector<T>& v)const {
		rep(i, sz(v)) { if (i)p(D.d); p(v[i]); }
	}
	template<class T>void p(const vector<vector<T>>& v)const {
		rep(i, sz(v)) { if (i)p(D.l); p(v[i]); }
	}
public:
	Output& operator()() {
		p(D.l); return *this;
	}
	template<class H>Output& operator()(H&& h) {
		p(h); p(D.l); return *this;
	}
	template<class H, class...T>Output& operator()(H&& h, T&& ...t) {
		p(h); p(D.d); return operator()(forward<T>(t)...);
	}
	template<class It>Output& range(const It& l, const It& r) {
		for (It i = l; i != r; i++) { if (i != l)p(D.d); p(*i); } p(D.l); return *this;
	}
	template<class T>Output& range(const T& a) {
		range(a.begin(), a.end()); return *this;
	}
	template<class...T>void exit(T&& ...t) {
		operator()(forward<T>(t)...); std::exit(EXIT_SUCCESS);
	}
	Output& flush() {
		fflush_unlocked(stdout); return *this;
	}
	Output& set(const BoolStr& b) {
		B = b; return *this;
	}
	Output& set(const DivStr& d) {
		D = d; return *this;
	}
	Output& set(const char* t, const char* f) {
		B = BoolStr(t, f); return *this;
	}
}out;

// --- step --- //
template<class T>struct Step {
	class It {
		T a, b, c;
	public:
		constexpr It() : a(T()), b(T()), c(T()) {}
		constexpr It(T _b, T _c, T _s) : a(_b), b(_c), c(_s) {}
		constexpr It& operator++() {
			--b; a += c; return *this;
		}
		constexpr It operator++(int) {
			It tmp = *this; --b; a += c; return tmp;
		}
		constexpr const T& operator*()const {
			return a;
		}
		constexpr const T* operator->()const {
			return &a;
		}
		constexpr bool operator==(const It& i)const {
			return b == i.b;
		}
		constexpr bool operator!=(const It& i)const {
			return !(b == i.b);
		}
		constexpr T start()const {
			return a;
		}
		constexpr T count()const {
			return b;
		}
		constexpr T step()const {
			return c;
		}
	};
	constexpr Step(T b, T c, T s) : be(b, c, s) {}
	constexpr It begin()const {
		return be;
	}
	constexpr It end()const {
		return en;
	}
	constexpr T start()const {
		return be.start();
	}
	constexpr T count()const {
		return be.count();
	}
	constexpr T step()const {
		return be.step();
	}
	constexpr T sum()const {
		return start() * count() + step() * (count() * (count() - 1) / 2);
	}
	operator vector<T>()const {
		return as_vector();
	}
	template<class F>void each(const F& f)const {
		for (T i : *this)f(i);
	}
	auto as_vector()const {
		vector<T> res; res.reserve(count()); each([&](T i) {res.push_back(i); }); return res;
	}
	template<class F, class U = decay_t<result_of_t<F(T)>>>auto map(const F& f)const {
		vector<U> res; res.reserve(count()); each([&](T i) {res.push_back(f(i)); }); return res;
	}
	template<class F>auto select(const F& f)const {
		vector<T> res; each([&](T i) {if (f(i))res.push_back(i); }); return res;
	}
	template<class F>int count_if(const F& f)const {
		int res = 0; each([&](T i) {if (f(i))++res; }); return res;
	}
	template<class F>T find_if(const F& f)const {
		for (T i : *this)if (f(i))return i; return 0;
	}
	template<class F>auto max_by(const F& f)const {
		auto v = map(f); return *max_element(v.begin(), v.end());
	}
	template<class F>auto min_by(const F& f)const {
		auto v = map(f); return *min_element(v.begin(), v.end());
	}
	template<class F>bool all_of(const F& f)const {
		for (T i : *this)if (!f(i))return false; return true;
	}
	template<class F>bool any_of(const F& f)const {
		for (T i : *this)if (f(i))return true; return false;
	}
	template<class F, class U = decay_t<result_of_t<F(T)>>>auto sum(const F& f)const {
		U res = 0; each([&](T i) {res += static_cast<U>(f(i)); }); return res;
	}
	using value_type = T;
	using iterator = It;
private:
	It be, en;
};
template<class T>inline constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template<class T>inline constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template<class T>inline constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}

// --- functions --- //
inline namespace {
	template<class T>inline void Sort(T& a) {
		sort(all(a));
	}
	template<class T>inline void RSort(T& a) {
		sort(rall(a));
	}
	template<class T, class F>inline void Sort(T& a, const F& f) {
		sort(all(a), f);
	}
	template<class T, class F>inline void RSort(T& a, const F& f) {
		sort(rall(a), f);
	}
	template<class T>inline T Sorted(T a) {
		Sort(a); return a;
	}
	template<class T>inline T RSorted(T a) {
		RSort(a); return a;
	}
	template<class T, class F>inline T Sorted(T& a, const F& f) {
		Sort(a, f); return a;
	}
	template<class T, class F>inline T RSorted(T& a, const F& f) {
		RSort(a, f); return a;
	}
	template<class T, class F>inline void SortBy(T& a, const F& f) {
		sort(all(a), [&](const auto& x, const auto& y) {return f(x) < f(y); });
	}
	template<class T, class F>inline void RSortBy(T& a, const F& f) {
		sort(rall(a), [&](const auto& x, const auto& y) {return f(x) < f(y); });
	}
	template<class T>inline void Reverse(T& a) {
		reverse(all(a));
	}
	template<class T>inline void Unique(T& a) {
		a.erase(unique(all(a)), a.end());
	}
	template<class T>inline void Uniq(T& a) {
		Sort(a); Unique(a);
	}
	template<class T>inline void Rotate(T& a, int left) {
		rotate(a.begin(), a.begin() + left, a.end());
	}
	template<class T>inline T Reversed(T a) {
		Reverse(a); return a;
	}
	template<class T>inline T Uniqued(T a) {
		Unique(a); return a;
	}
	template<class T>inline T Uniqed(T a) {
		Uniq(a); return a;
	}
	template<class T>inline T Rotated(T a, int left) {
		Rotate(a, left); return a;
	}
	template<class T>inline auto Max(const T& a) {
		return *max_element(all(a));
	}
	template<class T>inline auto Min(const T& a) {
		return *min_element(all(a));
	}
	template<class T>inline int MaxPos(const T& a) {
		return max_element(all(a)) - a.begin();
	}
	template<class T>inline int MinPos(const T& a) {
		return min_element(all(a)) - a.begin();
	}
	template<class T, class F>inline auto MaxBy(const T& a, const F& f) {
		return *max_element(all(a), [&](const auto& x, const auto& y) {return f(x) < f(y); });
	}
	template<class T, class F>inline auto MinBy(const T& a, const F& f) {
		return *min_element(all(a), [&](const auto& x, const auto& y) {return f(x) < f(y); });
	}
	template<class T, class U>inline int Count(const T& a, const U& v) {
		return count(all(a), v);
	}
	template<class T, class F>inline int CountIf(const T& a, const F& f) {
		return count_if(all(a), f);
	}
	template<class T, class U>inline int Find(const T& a, const U& v) {
		return find(all(a), v) - a.begin();
	}
	template<class T, class F>inline int FindIf(const T& a, const F& f) {
		return find_if(all(a), f) - a.begin();
	}
	template<class T, class U = typename T::value_type>inline U Sum(const T& a) {
		return accumulate(all(a), U());
	}
	template<class T, class U>inline bool Includes(const T& a, const U& v) {
		return find(all(a), v) != a.end();
	}
	template<class T, class F>inline auto Sum(const T& v, const F& f) {
		return accumulate(next(v.begin()), v.end(), f(*v.begin()), [&](auto a, auto b) {return a + f(b); });
	}
	template<class T, class U>inline int Lower(const T& a, const U& v) {
		return lower_bound(all(a), v) - a.begin();
	}
	template<class T, class U>inline int Upper(const T& a, const U& v) {
		return upper_bound(all(a), v) - a.begin();
	}
	template<class T, class F>inline void RemoveIf(T& a, const F& f) {
		a.erase(remove_if(all(a), f), a.end());
	}
	template<class F>inline auto Vector(size_t size, const F& f) {
		vector<decay_t<result_of_t<F(size_t)>>> res(size);
		for (size_t i = 0; i < size; ++i)res[i] = f(i);
		return res;
	}
	template<class T>inline auto Grid(size_t h, size_t w, const T& v = T()) {
		return vector<vector<T>>(h, vector<T>(w, v));
	}
	template<class T>inline auto Slice(const T& v, size_t i, size_t len) {
		return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
	}
	template<class T, class F>inline auto Each(const T& v, F&& f) {
		for (auto& i : v)f(i);
	}
	template<class T, class F>inline auto Select(const T& v, const F& f) {
		T res;
		for (const auto& e : v)if (f(e))res.push_back(e);
		return res;
	}
	template<class T, class F>inline auto Map(const T& v, F&& f) {
		vector<decay_t<result_of_t<F(typename T::value_type)>>> res(v.size());
		size_t i = 0; for (const auto& e : v)res[i++] = f(e);
		return res;
	}
	template<class T, class F>inline auto MapIndex(const T& v, const F& f) {
		vector<decay_t<result_of_t<F(size_t, typename T::value_type)>>> res(v.size());
		size_t i = 0; for (auto it = v.begin(); it != v.end(); ++it, ++i)res[i] = f(i, *it);
		return res;
	}
	template<class T, class F>inline auto TrueIndex(const T& v, const F& f) {
		vector<size_t> res;
		for (size_t i = 0; i < v.size(); ++i)if (f(v[i]))res.push_back(i);
		return res;
	}
	template<class T, class U = typename T::value_type>inline auto Indexed(const T& v) {
		vector<pair<U, int>> res(v.size());
		for (int i = 0; i < (int)v.size(); ++i)res[i] = make_pair(static_cast<U>(v[i]), i);
		return res;
	}
	inline auto operator*(string s, size_t n) {
		string res;
		for (size_t i = 0; i < n; ++i)res += s;
		return res;
	}
	template<class T>inline auto& operator<<(vector<T>& v, const vector<T>& v2) {
		v.insert(v.end(), all(v2)); return v;
	}
	template<class T>inline T Ceil(T n, T m) {
		return (n + m - 1) / m;
	}
	template<class T>inline T Ceil2(T n, T m) {
		return Ceil(n, m) * m;
	}
	template<class T>inline T Tri(T n) {
		return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
	}
	template<class T>inline T nC2(T n) {
		return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
	}
	template<class T>inline T Mid(const T& l, const T& r) {
		return l + (r - l) / 2;
	}
	inline int pop_count(int n) {
		return bitset<32>(n).count();
	}
	inline int pop_count(ll n) {
		return bitset<64>(n).count();
	}
	template<class T>inline bool chmax(T& a, const T& b) {
		if (a < b) { a = b; return true; } return false;
	}
	template<class T>inline bool chmin(T& a, const T& b) {
		if (a > b) { a = b; return true; } return false;
	}
	template<class T>inline bool inRange(const T& v, const T& min, const T& max) {
		return min <= v && v < max;
	}
	template<class T>inline bool isSquere(T n) {
		T s = sqrt(n); return s * s == n || (s + 1) * (s + 1) == n;
	}
	template<class T = ll>inline T BIT(int b) {
		return T(1) << b;
	}
	template<class T>inline T Gcd(T n, T m) {
		return m ? Gcd(m, n % m) : n;
	}
	template<class T>inline T Lcm(T n, T m) {
		return n / Gcd(n, m) * m;
	}
	template<class T, class U = typename T::value_type>inline U Gcdv(const T& v) {
		return accumulate(next(v.begin()), v.end(), U(*v.begin()), Gcd<U>);
	}
	template<class T, class U = typename T::value_type>inline U Lcmv(const T& v) {
		return accumulate(next(v.begin()), v.end(), U(*v.begin()), Lcm<U>);
	}
	template<class T>inline T Pow(T a, T n) {
		T r = 1; while (n > 0) { if (n & 1)r *= a; a *= a; n /= 2; } return r;
	}
	template<class T>inline T Powmod(T a, T n, T m = MOD) {
		T r = 1; while (n > 0) { if (n & 1)r = r * a % m, n--; else a = a * a % m, n /= 2; }return r;
	}
}

// --- dump --- //
#if __has_include("dump.hpp")
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

// ---------------------------------------------------------------- //

template<class T>class CulSum2D {
	int h, w; vector<vector<T>> s;
public:
	CulSum2D(const vector<vector<T>>& a) :h(a.size()), w(a.front().size()), s(h + 1, vector<T>(w + 1)) {
		for (int i = 0; i < h; ++i)for (int j = 0; j < w; ++j)
			s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + a[i][j];
	}
	template<class U, class F>
	CulSum2D(const U& a, F f) : h(a.size()), w(a.front().size()), s(h + 1, vector<T>(w + 1)) {
		for (int i = 0; i < h; ++i)for (int j = 0; j < w; ++j)
			s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + static_cast<T>(f(a[i][j]));
	}
	// [y1, y2) * [x1, x2)
	T operator()(int y1, int y2, int x1, int x2) {
		x1 = min(max(x1, 0), w); x2 = min(max(x2, 0), w);
		y1 = min(max(y1, 0), h); y2 = min(max(y2, 0), h);
		return (x1 > x2 || y1 > y2) ? 0 : s[y2][x2] - s[y1][x2] - s[y2][x1] + s[y1][x1];
	}
	// [0, y) * [0, x)
	T operator()(int y, int x) {
		x = min(max(x, 0), w); y = min(max(y, 0), h);
		return s[y][x];
	}
	const vector<vector<T>>& get_s() { return s; }
};

int main() {
	int n = in;
	VVI a = in[{n, n}];
	CulSum2D<int> sum(a);
	int ans = -inf;
	rep(y1, n)FOR(y2, y1, n)rep(x1, n)FOR(x2, x1, n) {
		chmax(ans, sum(y1, y2 + 1, x1, x2 + 1));
	}
	out(ans);
}
accuracy: 1
time_ms: 30
memory_kb: 3252
final_rank: 10

code_snippet: #include <iostream>
#include <algorithm>
#include <iomanip>
#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;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> v(n+1, vector<int>(n+1, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &v[i+1][j+1]);
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= n; ++j) {
            v[i+1][j] += v[i][j];
        }
    }
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j < n; ++j) {
            v[i][j+1] += v[i][j];
        }
    }
    int ans = -INF<int>;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = i+1; k <= n; ++k) {
                for (int l = j+1; l <= n; ++l) {
                    ans = max(ans, v[k][l]-v[k][j]-v[i][l]+v[i][j]);
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3276
final_rank: 7

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

const double pi = 3.141592653589793238462643383279;

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s)
{
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x)
{
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline int readInt()
{
  int x;
  scanf("%d", &x);
  return x;
}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#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 sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

class UnionFind
{
public:
  vector<int> par;
  vector<int> siz;

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

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

  bool merge(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      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)];
  }
};

ll modPow(ll x, ll n, ll mod = MOD)
{
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = (res * x) % mod;

    res %= mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve()
{
  for (int i = 0; i < SIEVE_SIZE; ++i)
    sieve[i] = true;
  sieve[0] = sieve[1] = false;
  for (int i = 2; i * i < SIEVE_SIZE; ++i)
    if (sieve[i])
      for (int j = 2; i * j < SIEVE_SIZE; ++j)
        sieve[i * j] = false;
}

bool isprime(ll n)
{
  if (n == 0 || n == 1)
    return false;
  for (ll i = 2; i * i <= n; ++i)
    if (n % i == 0)
      return false;
  return true;
}

const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit()
{
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++)
  {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k)
{
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long extGCD(long long a, long long b, long long &x, long long &y)
{
  if (b == 0)
  {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m)
{
  return (a % m + m) % m;
}

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m)
{
  long long x, y;
  extGCD(a, m, x, y);
  return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b)
{

  if (b == 0)
    return a;
  return GCD(b, a % b);
}

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B)
{
  mat C(A.size(), vec((int)B[0].size()));
  for (int i = 0; i < A.size(); ++i)
  {
    for (int k = 0; k < B.size(); ++k)
    {
      for (int j = 0; j < B[0].size(); ++j)
      {
        C[i][j] = (C[i][j] + A[i][k] % MOD * B[k][j] % MOD) % MOD;
      }
    }
  }
  return C;
}
mat matPow(mat A, ll n)
{
  mat B(A.size(), vec((int)A.size()));

  for (int i = 0; i < A.size(); ++i)
  {
    B[i][i] = 1;
  }

  while (n > 0)
  {
    if (n & 1)
      B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}

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

  int n;
  cin >> n;
  vector<vector<ll>> a(n + 2, vector<ll>(n + 2));
  REP(i, n)
  {
    REP(j, n)
    {
      cin >> a[i + 1][j + 1];
    }
  }

  REP(i, n + 1)
  {
    REP(j, n + 1)
    {
      a[i][j + 1] += a[i][j];
    }
  }
  REP(j, n + 1)
  {
    REP(i, n + 1)
    {
      a[i + 1][j] += a[i][j];
    }
  }
  // REP(i, n + 1)
  // {
  //   REP(j, n + 1)
  //   {
  //     cout << a[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  ll mx = -(1LL << 60);
  for (int i = 0; i <= n; i++)
  {
    for (int j = 0; j <= n; j++)
    {
      for (int k = i + 1; k <= n; k++)
      {
        for (int l = j + 1; l <= n; l++)
        {
          mx = max(mx, a[k][l] - a[k][j] - a[i][l] + a[i][j]);
        }
      }
    }
  }
  cout << mx << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3272
final_rank: 6