#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define vll vector <ll>
#define vbool vector<bool>
#define pairll pair<ll,ll>
#define fi first
#define sc second
#define rever greater<ll>()
using namespace std;
void solve(){
ll n, m, a, b; cin >> n >> m >> a >> b;
vector<vll> c(n+1, vll(m));
for(ll i = 1; i <= n; i++) {
for(ll j = 0; j < m; j++){
cin >> c[i][j]; c[i][j] += c[i-1][j];
}
}
if(a > b)swap(a,b);
ll res = 1e18;
for(ll i = 1; i <= n; i++){
for(ll k = 1; k <= i; k++){
ll tot = 0;
for(ll l=0, r=0; r < m; r++){
tot += c[i][r]-c[i-k][r];
while(tot > b){
tot -= c[i][l]-c[i-k][l]; l++;
}res = min(res, abs(tot-a)+abs(tot-b));
}
}
}cout << res << endl;
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
ll t=1; //cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |