# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852225 | Trisanu_Das | Zemljište (COCI22_zemljiste) | C++17 | 0 ms | 0 KiB |
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 a[r][s]; for(int i = 0; i < r; i++) for(int j = 0; j < s; j++) cin >> a[i][j];
for(int i = 1; i < r; i++) for(int j = 0; j < s; j++) a[i][j] += a[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 += a[j][k] - a[i - 1][k];
while(sum > b && pos <= k){
ans = min(ans, abs(b - sum) + abs(sum - a));
sum -= a[j][pos] - a[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';
}