This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
using namespace std;
#define int long long
signed main() {
int r, s, a, b;
cin >> r >> s >> a >> b;
if (b > a) swap (a, b);
vector<vector<int>> grid (r+1,vector<int> (s+1));
for (int i = 0; i < r; ++i) {
for (int j = 0; j < s; ++j) {
int x;
cin >> x;
grid[i+1][j+1]=grid[i+1][j]+grid[i][j+1]-grid[i][j]+x;
}
}
int sol = 1e18;
for (int i = 1; i <= r; ++i) {
for (int j = 0; j < i; ++j) {
int l = 0, r = 1;
while (r <= s) {
while (l < r and grid[i][r]-grid[j+1][r]-grid[i][l+1]+grid[j][l] > b) {
int val = grid[i][r]-grid[j+1][r]-grid[i][l+1]+grid[j][l];
sol = min (sol, abs (a-val)+abs (b-val));
++l;
}
if (l < r) {
int val = grid[i][r]-grid[j+1][r]-grid[i][l+1]+grid[j][l];
//cout << grid[j][l+1] << ' ';
sol = min (sol, abs (a-val)+abs (b-val));
}
++r;
}
}
}
cout << sol << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |