#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;
ll mn = 1e18;
for (ll i = 1; i <= n; i ++){
for (ll j = 1; j <= m; j ++){
cin >> mat[i][j];
if (mat[i][j] > b)
mn = min(mn, 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);
ans = min(ans, abs(sm - a) + abs(sm - b));
}
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
13 ms |
604 KB |
Output is correct |
11 |
Correct |
44 ms |
860 KB |
Output is correct |
12 |
Correct |
54 ms |
916 KB |
Output is correct |
13 |
Correct |
54 ms |
856 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
49 ms |
1108 KB |
Output is correct |
16 |
Correct |
44 ms |
856 KB |
Output is correct |
17 |
Correct |
43 ms |
860 KB |
Output is correct |
18 |
Correct |
43 ms |
856 KB |
Output is correct |
19 |
Correct |
43 ms |
856 KB |
Output is correct |
20 |
Correct |
51 ms |
856 KB |
Output is correct |
21 |
Correct |
42 ms |
856 KB |
Output is correct |
22 |
Correct |
46 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
13 ms |
604 KB |
Output is correct |
11 |
Correct |
44 ms |
860 KB |
Output is correct |
12 |
Correct |
54 ms |
916 KB |
Output is correct |
13 |
Correct |
54 ms |
856 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
49 ms |
1108 KB |
Output is correct |
16 |
Correct |
44 ms |
856 KB |
Output is correct |
17 |
Correct |
43 ms |
860 KB |
Output is correct |
18 |
Correct |
43 ms |
856 KB |
Output is correct |
19 |
Correct |
43 ms |
856 KB |
Output is correct |
20 |
Correct |
51 ms |
856 KB |
Output is correct |
21 |
Correct |
42 ms |
856 KB |
Output is correct |
22 |
Correct |
46 ms |
860 KB |
Output is correct |
23 |
Execution timed out |
2077 ms |
4752 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |