#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#define ll long long
using namespace std;
int main(){
int r,s;
cin>>r>>s;
ll a,b;
cin>>a>>b;
if(a>b) swap(a,b);
vector<vector<ll> > vec(r,vector<ll>(s)),pree(r,vector<ll>(s));
for(int i=0;i<r;i++) for(int j=0;j<s;j++) cin>>vec[i][j];
for(int i=0;i<s;i++){
pree[0][i]=vec[0][i];
for(int j=1;j<r;j++) pree[j][i]=pree[j-1][i]+vec[j][i];
}
ll ans=1e10;
for(int i=0;i<r;i++){
for(int j=i;j<r;j++){
vector<ll> pre(s+1);
if(i==0) for(int k=0;k<s;k++) pre[k+1]=pre[k]+pree[j][k];
else for(int k=0;k<s;k++) pre[k+1]=pre[k]+(pree[j][k]-pree[i-1][k]);
for(int le=0,rt=1;le<s&&rt<=s;le++){
while(rt<=s&&pre[rt]-pre[le]<a) rt++;
if(rt<=s){
if(pre[rt]-pre[le]<=b) ans=b-a;
else ans=min(ans,2*(pre[rt]-pre[le])-b-a);
}
if(rt-1>le){
ans=min(ans,b+a-2*(pre[rt-1]-pre[le]));
}
}
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
600 KB |
Output is correct |
16 |
Correct |
6 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
600 KB |
Output is correct |
19 |
Correct |
3 ms |
600 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
600 KB |
Output is correct |
16 |
Correct |
6 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
600 KB |
Output is correct |
19 |
Correct |
3 ms |
600 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
3 ms |
600 KB |
Output is correct |
23 |
Correct |
257 ms |
6784 KB |
Output is correct |
24 |
Correct |
256 ms |
6652 KB |
Output is correct |
25 |
Correct |
260 ms |
6788 KB |
Output is correct |
26 |
Correct |
261 ms |
6784 KB |
Output is correct |
27 |
Correct |
177 ms |
4432 KB |
Output is correct |
28 |
Correct |
259 ms |
6736 KB |
Output is correct |
29 |
Correct |
254 ms |
6740 KB |
Output is correct |
30 |
Correct |
257 ms |
6736 KB |
Output is correct |
31 |
Correct |
36 ms |
1112 KB |
Output is correct |
32 |
Correct |
247 ms |
6736 KB |
Output is correct |
33 |
Correct |
264 ms |
6992 KB |
Output is correct |
34 |
Correct |
248 ms |
6784 KB |
Output is correct |
35 |
Correct |
251 ms |
6788 KB |
Output is correct |
36 |
Correct |
249 ms |
6736 KB |
Output is correct |
37 |
Correct |
254 ms |
6788 KB |
Output is correct |
38 |
Correct |
249 ms |
6736 KB |
Output is correct |
39 |
Correct |
255 ms |
6792 KB |
Output is correct |
40 |
Correct |
254 ms |
6736 KB |
Output is correct |
41 |
Correct |
130 ms |
5520 KB |
Output is correct |
42 |
Correct |
127 ms |
5460 KB |
Output is correct |
43 |
Correct |
137 ms |
5524 KB |
Output is correct |
44 |
Correct |
128 ms |
5520 KB |
Output is correct |