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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll pref[501][501], n, m, a, b;
ll dist (ll x) {
return abs(x - a) + abs(x - b);
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> a >> b;
if (a > b) swap(a, b);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> pref[i][j]; pref[i][j] += pref[i - 1][j];
}
}
ll mn = 1e18;
for (int l = 1; l <= n; l++) {
for (int r = l; r <= n; r++) {
ll arr[m + 1] = {};
for (int j = 1; j <= m; j++) {
arr[j] = pref[r][j] - pref[l - 1][j];
arr[j] += arr[j - 1];
}
int ptr = 1;
for (int i = 1; i <= m; i++) {
while (arr[i] - arr[ptr] >= a && ptr < i) ptr++;
mn = min(mn, dist(arr[i] - arr[ptr]));
if (ptr) mn = min(mn, dist(arr[i] - arr[ptr - 1]));
}
}
}
cout << mn << '\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... |