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;
int main(){
int r, s, a, b; cin >> r >> s >> a >> b;
int v[r][s]; for(int i = 0; i < r; i++) for(int j = 0; j < s; j++) cin >> v[i][j];
for(int i = 1; i < r; i++) for(int j = 0; j < s; j++) v[i][j] += v[i - 1][j];
int ans = INT_MAX;
for(int i = 0; i < r; i++){
for(int j = i; j < r; j++){
int pos = 0, sum = 0;
for(int k = 0; k < s; k++){
sum += v[j][k] - v[i - 1][k];
while(sum > b && pos <= k){
ans = min(ans, abs(b - sum) + abs(sum - a));
sum -= v[j][pos] - v[i - 1][pos];
pos++;
}
if(a <= sum && b >= sum){
cout << b - a << '\n'; return 0;
}
ans = min(ans, abs(b - sum) + abs(sum - a));
}
}
}
cout << ans << '\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... |