#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 505;
int n, m, a, b, i, j, x, y;
ll mat[N][N], sm, tmp, ans;
ll f(int y){
return mat[x][y] - mat[x][j - 1] - mat[i - 1][y] + mat[i - 1][j - 1];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> a >> b;
for (i = 1; i <= n; i ++){
for (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];
}
}
if (b < a) swap(a, b);
ans = 1e18;
for (i = 1; i <= n; i ++){
for (j = 1; j <= m; j ++){
y = j;
for (x = i; x <= n; x ++){
while (y + 1 <= m and f(y + 1) <= b)
y++;
sm = f(y);
tmp = abs(a - sm) + abs(b - sm);
ans = min(ans, tmp);
if (y < m) sm = f(y + 1);
ans = min(ans, abs(a - sm) + abs(b - sm));
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
736 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
6 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
964 KB |
Output is correct |
17 |
Correct |
6 ms |
860 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
4 ms |
908 KB |
Output is correct |
20 |
Correct |
4 ms |
728 KB |
Output is correct |
21 |
Correct |
5 ms |
856 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
736 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
6 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
964 KB |
Output is correct |
17 |
Correct |
6 ms |
860 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
4 ms |
908 KB |
Output is correct |
20 |
Correct |
4 ms |
728 KB |
Output is correct |
21 |
Correct |
5 ms |
856 KB |
Output is correct |
22 |
Correct |
5 ms |
860 KB |
Output is correct |
23 |
Correct |
645 ms |
4836 KB |
Output is correct |
24 |
Correct |
650 ms |
4824 KB |
Output is correct |
25 |
Correct |
681 ms |
4944 KB |
Output is correct |
26 |
Correct |
688 ms |
4824 KB |
Output is correct |
27 |
Correct |
414 ms |
3932 KB |
Output is correct |
28 |
Correct |
639 ms |
4832 KB |
Output is correct |
29 |
Correct |
635 ms |
4832 KB |
Output is correct |
30 |
Correct |
630 ms |
4700 KB |
Output is correct |
31 |
Correct |
85 ms |
2396 KB |
Output is correct |
32 |
Correct |
688 ms |
4832 KB |
Output is correct |
33 |
Correct |
698 ms |
4836 KB |
Output is correct |
34 |
Correct |
652 ms |
4832 KB |
Output is correct |
35 |
Correct |
636 ms |
4832 KB |
Output is correct |
36 |
Correct |
657 ms |
4832 KB |
Output is correct |
37 |
Correct |
621 ms |
4688 KB |
Output is correct |
38 |
Incorrect |
699 ms |
4828 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |