// fixa a linha de começo e fim e uma coluna
// a coluna da prox vai ser maior ou igual a anterior
#include<bits/stdc++.h>
#define int long long
using namespace std ;
const int maxn = 505 ;
const int inf = 1e18 ;
int n, m, a, b, v[maxn][maxn], pref[maxn][maxn] ;
int32_t main(){
cin >> n >> m >> a >> b ;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
cin >> v[i][j] ;
}
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
pref[i][j] = pref[i-1][j] + pref[i][j-1] + v[i][j] - pref[i-1][j-1] ;
}
}
int ans = inf ;
for(int i = 1 ; i <= n ; i++){
for(int f_i = i ; f_i <= n ; f_i++){
for(int j = 1, f_j = j ; j <= m ; j++){
while(f_j < m && pref[f_i][f_j] - pref[f_i][j-1] - pref[i-1][f_j] + pref[i-1][j-1] < a){
int val = pref[f_i][f_j] - pref[f_i][j-1] - pref[i-1][f_j] + pref[i-1][j-1] ;
ans = min(ans, abs(val - a) + abs(val - b)) ;
f_j++ ;
}
int val = pref[f_i][f_j] - pref[f_i][j-1] - pref[i-1][f_j] + pref[i-1][j-1] ;
ans = min(ans, abs(val - a) + abs(val - b)) ;
}
}
}
cout << ans << "\n" ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
7 ms |
1128 KB |
Output is correct |
12 |
Correct |
8 ms |
1204 KB |
Output is correct |
13 |
Correct |
8 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
452 KB |
Output is correct |
15 |
Correct |
10 ms |
1204 KB |
Output is correct |
16 |
Correct |
8 ms |
1168 KB |
Output is correct |
17 |
Correct |
7 ms |
1108 KB |
Output is correct |
18 |
Correct |
7 ms |
1148 KB |
Output is correct |
19 |
Correct |
6 ms |
1108 KB |
Output is correct |
20 |
Correct |
5 ms |
1084 KB |
Output is correct |
21 |
Correct |
7 ms |
1108 KB |
Output is correct |
22 |
Correct |
5 ms |
1088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
7 ms |
1128 KB |
Output is correct |
12 |
Correct |
8 ms |
1204 KB |
Output is correct |
13 |
Correct |
8 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
452 KB |
Output is correct |
15 |
Correct |
10 ms |
1204 KB |
Output is correct |
16 |
Correct |
8 ms |
1168 KB |
Output is correct |
17 |
Correct |
7 ms |
1108 KB |
Output is correct |
18 |
Correct |
7 ms |
1148 KB |
Output is correct |
19 |
Correct |
6 ms |
1108 KB |
Output is correct |
20 |
Correct |
5 ms |
1084 KB |
Output is correct |
21 |
Correct |
7 ms |
1108 KB |
Output is correct |
22 |
Correct |
5 ms |
1088 KB |
Output is correct |
23 |
Correct |
404 ms |
6600 KB |
Output is correct |
24 |
Correct |
371 ms |
6680 KB |
Output is correct |
25 |
Correct |
375 ms |
6656 KB |
Output is correct |
26 |
Correct |
340 ms |
6604 KB |
Output is correct |
27 |
Correct |
229 ms |
5808 KB |
Output is correct |
28 |
Correct |
356 ms |
6660 KB |
Output is correct |
29 |
Correct |
339 ms |
6696 KB |
Output is correct |
30 |
Correct |
350 ms |
6660 KB |
Output is correct |
31 |
Correct |
52 ms |
4204 KB |
Output is correct |
32 |
Correct |
342 ms |
6656 KB |
Output is correct |
33 |
Correct |
356 ms |
6692 KB |
Output is correct |
34 |
Correct |
351 ms |
6652 KB |
Output is correct |
35 |
Correct |
344 ms |
6780 KB |
Output is correct |
36 |
Correct |
355 ms |
6664 KB |
Output is correct |
37 |
Correct |
338 ms |
6620 KB |
Output is correct |
38 |
Correct |
361 ms |
6660 KB |
Output is correct |
39 |
Correct |
340 ms |
6660 KB |
Output is correct |
40 |
Correct |
351 ms |
6656 KB |
Output is correct |
41 |
Correct |
308 ms |
5304 KB |
Output is correct |
42 |
Correct |
311 ms |
5392 KB |
Output is correct |
43 |
Correct |
320 ms |
5396 KB |
Output is correct |
44 |
Correct |
318 ms |
5364 KB |
Output is correct |