#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 505;
ll n, m, a, b, mat[N][N];
ll f(ll i, ll j, ll x, ll y){
return mat[x][y] - mat[x][j - 1] - mat[i - 1][y] + mat[i - 1][j - 1];
}
int main(){
cin >> n >> m >> a >> b;
for (ll i = 1; i <= n; i ++){
for (ll j = 1; j <= m; j ++){
cin >> mat[i][j];
mat[i][j] += mat[i - 1][j];
mat[i][j] += mat[i][j - 1];
mat[i][j] -= mat[i - 1][j - 1];
}
}
ll ans = 1e18;
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= m; j ++){
for (int x = i; x <= n; x ++){
for (int y = j; y <= m; y ++){
ll sm = f(i, j, x, y);
if (sm > b) continue;
ans = min(ans, abs(sm - a) + abs(sm - b));
if (y < m) sm = f(i, j, x, y + 1);
ans = min(ans, abs(sm - a) + abs(sm - b));
}
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |