#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define all(x) x.begin(),x.end()
const ll mxn=505;
ll pre[mxn][mxn];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n,m;
ll a,b;
cin>>n>>m>>a>>b;
if(a>b) swap(a,b);
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
cin>>pre[i][j];
pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
}
}
ll ans=1e18;
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j++){
vector<ll> vec;
for(ll k=0;k<=m;k++){
vec.push_back(pre[j][k]-pre[i-1][k]);
}
for(int k=0;k<=m;k++){
ll aa=vec[k]+a;
ll bb=vec[k]+b;
auto it1=lower_bound(all(vec),aa);
auto it2=upper_bound(all(vec),bb);
it2--;
if(it1!=vec.end()){
ll cur=(*it1)-vec[k];
ans=min(ans,abs(cur-a)+abs(cur-b));
}
if(it2-vec.begin()>k){
ll cur=(*it2)-vec[k];
ans=min(ans,abs(cur-a)+abs(cur-b));
}
}
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
15 ms |
728 KB |
Output is correct |
12 |
Correct |
14 ms |
724 KB |
Output is correct |
13 |
Correct |
15 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
724 KB |
Output is correct |
16 |
Correct |
14 ms |
724 KB |
Output is correct |
17 |
Correct |
15 ms |
724 KB |
Output is correct |
18 |
Correct |
10 ms |
724 KB |
Output is correct |
19 |
Correct |
10 ms |
728 KB |
Output is correct |
20 |
Correct |
10 ms |
724 KB |
Output is correct |
21 |
Correct |
10 ms |
724 KB |
Output is correct |
22 |
Correct |
10 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
15 ms |
728 KB |
Output is correct |
12 |
Correct |
14 ms |
724 KB |
Output is correct |
13 |
Correct |
15 ms |
732 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
724 KB |
Output is correct |
16 |
Correct |
14 ms |
724 KB |
Output is correct |
17 |
Correct |
15 ms |
724 KB |
Output is correct |
18 |
Correct |
10 ms |
724 KB |
Output is correct |
19 |
Correct |
10 ms |
728 KB |
Output is correct |
20 |
Correct |
10 ms |
724 KB |
Output is correct |
21 |
Correct |
10 ms |
724 KB |
Output is correct |
22 |
Correct |
10 ms |
724 KB |
Output is correct |
23 |
Execution timed out |
2072 ms |
2288 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |