#include <bits/stdc++.h>
using namespace std;
#define int long long
int arr[505][505];
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int r, s, a, b; cin >> r >> s >> a >> b;
if(a > b) swap(a, b);
for(int i = 1; i <= r; i++) for(int j = 1;j <= s; j++) cin >> arr[i][j];
for(int j = 1; j <= s; j++) for(int i = 1; i <= r;i++) arr[i][j] += arr[i - 1][j];
int ans = INT_MAX;
for(int i = 1; i <= r; i++){
for(int j = i; j <= r; j++){
int pos = 0, sum = 0;
for(int k = 1; k <= s; k++){
sum += arr[j][k] - arr[i - 1][k];
while(sum > b && pos <= k){
ans = min(abs(a - sum) + abs(b - sum), ans);
sum -= arr[j][pos] - arr[i - 1][pos];
pos++;
}
if(a <= sum && b >= sum){
cout << b - a << '\n'; return 0;
}
ans = min(abs(a - sum) + abs(b - sum), ans);
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
764 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
732 KB |
Output is correct |
13 |
Correct |
1 ms |
764 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
22 ms |
4812 KB |
Output is correct |
24 |
Correct |
23 ms |
4696 KB |
Output is correct |
25 |
Correct |
20 ms |
4688 KB |
Output is correct |
26 |
Correct |
21 ms |
4792 KB |
Output is correct |
27 |
Correct |
16 ms |
3872 KB |
Output is correct |
28 |
Correct |
25 ms |
4668 KB |
Output is correct |
29 |
Correct |
24 ms |
4700 KB |
Output is correct |
30 |
Correct |
20 ms |
4700 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
226 ms |
4692 KB |
Output is correct |
33 |
Correct |
224 ms |
4836 KB |
Output is correct |
34 |
Correct |
226 ms |
4692 KB |
Output is correct |
35 |
Correct |
224 ms |
4840 KB |
Output is correct |
36 |
Correct |
241 ms |
4836 KB |
Output is correct |
37 |
Correct |
225 ms |
4836 KB |
Output is correct |
38 |
Correct |
229 ms |
4692 KB |
Output is correct |
39 |
Correct |
233 ms |
4948 KB |
Output is correct |
40 |
Correct |
227 ms |
4748 KB |
Output is correct |
41 |
Correct |
20 ms |
3416 KB |
Output is correct |
42 |
Correct |
15 ms |
3420 KB |
Output is correct |
43 |
Correct |
15 ms |
3348 KB |
Output is correct |
44 |
Correct |
16 ms |
3540 KB |
Output is correct |