Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-12-05 21:34:33
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- vi a;
- int kadane(vi &a)
- {
- int n = a.size();
- int max_sum = INT_MIN;
- int sum = 0;
- for (int i = 0; i < n; i++)
- {
- sum += a[i];
- max_sum = max(max_sum, sum);
- if (sum < 0)
- {
- sum = 0;
- }
- }
- return max_sum;
- }
- void solve()
- {
- // maximal sum rectangle
- int n;
- cin >> n;
- int grid[n][n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cin >> grid[i][j];
- }
- }
- // fix the left and move the right
- a.resize(n);
- int ans = INT_MIN;
- for (int l = 0; l < n; l++)
- {
- for (int i = 0; i < n; i++)
- {
- // reset the array
- a[i] = 0;
- }
- for (int r = l; r < n; r++)
- {
- for (int i = 0; i < n; i++)
- {
- a[i] += grid[i][r];
- }
- ans = max(ans, kadane(a));
- }
- }
- cout << ans << endl;
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment