Advertisement
UNoobAle

10827 - Maximum sum on a torus

Sep 29th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define FOR(i,a,b) for (ll i = a; i < b; ++i)
  5. #define REP(i,a,b) for (ll i = a; i <= b; ++i)
  6. #define INF 9999999999
  7. int main()
  8. {
  9.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  10.     //freopen("input.txt", "r", stdin);
  11.     //freopen("output.txt", "w", stdout);
  12.     int t; cin >> t; while (t--)
  13.     {
  14.         ll n, mx=0, tmp; cin >> n; ll dp[2*n+1][2*n+1];
  15.         REP(i,0,n) dp[0][i]=dp[i][0]=0;
  16.         REP(i,1,n) REP(j,1,n) cin >> tmp, dp[i][j]=dp[i+n][j]=dp[i][j+n]=dp[i+n][j+n]=tmp;
  17.         REP(i,1,2*n) REP(j,1,2*n) dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+dp[i][j];
  18.         REP(i,1,n) REP(j,1,n) REP(p,i,i+n-1) REP(q,j,j+n-1) mx=max(mx,dp[p][q]-(dp[i-1][q]+dp[p][j-1]-dp[i-1][j-1]));
  19.         cout << mx << endl;
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement