#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=1;k<=m;k++){
vec.push_back(pre[j][k]-pre[i-1][k]);
}
for(ll k=0;k<m;k++){
ll sum=pre[j][k]-pre[i-1][k];
ll aa=sum+a;
ll bb=sum+b;
auto it1=lower_bound(all(vec),aa);
auto it2=upper_bound(all(vec),bb);
it2--;
if(it1<=it2){
cout<<abs(b-a);
return 0;
}
if(it1-vec.begin()>=k and it1-vec.begin()<n){
ans=min(ans,abs((*it1)-sum-a)+abs((*it1)-sum-b));
}
if(it2-vec.begin()>=k and it2-vec.begin()<n){
ans=min(ans,abs((*it2)-sum-a)+abs((*it2)-sum-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 |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |