#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
ll arr[505][505];
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll 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];
}
}
ll ans = 1e18;
for(int l=1;l<=R;l++){
for(int r = l;r<=R;r++){
int p = 0;
ll sum = 0;
for(int i=1;i<=S;i++){
ll e = arr[r][i]-arr[l-1][i];
sum+=e;
while(sum>b && p<=i){
ans = min(abs(a-sum)+abs(b-sum),ans);
sum-=arr[r][p]-arr[l-1][p];
p++;
}
if(a<=sum && b>=sum){
//cout<<sum<<"k\n";
cout<<b-a<<"\n";
return 0;
}
ans = min(abs(a-sum)+abs(b-sum),ans);
}
}
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
728 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
728 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
732 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
736 KB |
Output is correct |
23 |
Correct |
29 ms |
4692 KB |
Output is correct |
24 |
Correct |
25 ms |
4620 KB |
Output is correct |
25 |
Correct |
24 ms |
4684 KB |
Output is correct |
26 |
Correct |
27 ms |
4768 KB |
Output is correct |
27 |
Correct |
18 ms |
3864 KB |
Output is correct |
28 |
Correct |
24 ms |
4648 KB |
Output is correct |
29 |
Correct |
26 ms |
4636 KB |
Output is correct |
30 |
Correct |
28 ms |
4748 KB |
Output is correct |
31 |
Correct |
6 ms |
2388 KB |
Output is correct |
32 |
Correct |
279 ms |
4704 KB |
Output is correct |
33 |
Correct |
279 ms |
4708 KB |
Output is correct |
34 |
Correct |
284 ms |
4708 KB |
Output is correct |
35 |
Correct |
280 ms |
4812 KB |
Output is correct |
36 |
Correct |
274 ms |
4760 KB |
Output is correct |
37 |
Correct |
269 ms |
4704 KB |
Output is correct |
38 |
Correct |
275 ms |
4716 KB |
Output is correct |
39 |
Correct |
273 ms |
4704 KB |
Output is correct |
40 |
Correct |
279 ms |
4632 KB |
Output is correct |
41 |
Correct |
18 ms |
3344 KB |
Output is correct |
42 |
Correct |
17 ms |
3404 KB |
Output is correct |
43 |
Correct |
17 ms |
3396 KB |
Output is correct |
44 |
Correct |
19 ms |
3412 KB |
Output is correct |