This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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]);
}
int r=1;
for(int l=0;l<m;l++){
while(r<=m and vec[r]-vec[l]<a){
ans=min(ans,abs(vec[r]-vec[l]-a)+abs(vec[r]-vec[l]-b));
r++;
}
if(l<r and r<=m){
ans=min(ans,abs(vec[r]-vec[l]-a)+abs(vec[r]-vec[l]-b));
}
}
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |