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;
typedef long long ll;
#define endl '\n'
vector<vector<ll>> v;
ll getSum(int i,int l,int r){
return v[i][r] - (l ? v[i][l-1] : 0);
}
void solve(){
int n,m,a,b;
cin>>n>>m>>a>>b;
v.resize(n,vector<ll>(m));
for(auto &i:v) for(auto &j:i) cin>>j;
for(int i=0;i<n;i++){
for(int j=1;j<m;j++) v[i][j] += v[i][j-1];
}
if(a > b) swap(a,b);
ll ans = 1e18;
for(int l=0;l<m;l++){
for(int r=l;r<m;r++){
vector<ll> prefix{0};
for(int i=0;i<n;i++) prefix.push_back(getSum(i,l,r));
for(int i=1;i<prefix.size();i++) prefix[i] += prefix[i-1];
int j=0;
ll ans2 = 0;
for(int i=1;i<=n;i++){
if(i>1 && j) ans2 += getSum(i-1,l,r);
while(j<i&&prefix[i]-prefix[j]>b) ans2 = prefix[i]-prefix[j] , j ++;
ll ans1 = j != i ? prefix[i] - prefix[j] : 1e18;
if(j) ans = min(ans , abs(ans2 - a) + abs(ans2 - b));
ans = min(abs(ans1-a)+abs(ans1-b) , ans);
}
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:23:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=1;i<prefix.size();i++) prefix[i] += prefix[i-1];
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |