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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
//DON'T BELIEVE ANYONE WILL INSPIRE YOU -> TRAIN HARDER -> YOU WILL GET WHAT YOU WANT
const int MAX = 503;
int N, M, A, B;
ll a[MAX][MAX], pre[MAX][MAX];
void input(){
cin >> N >> M >> A >> B;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
cin >> a[i][j];
pre[i][j] = pre[i - 1][j] + a[i][j];
}
}
if(A > B) swap(A, B);
}
void solve(){
ll ans = oo;
for(int u = 1; u <= N; u ++){
for(int d = u; d <= N; d++){
vector<ll> prefix(M + 3, 0);
for(int i = 1; i <= M; i++){
prefix[i] = prefix[i - 1] + pre[d][i] - pre[u - 1][i];
}
int j = 1, k = 1;
for(int i = 1; i <= M; i++){
while(prefix[i] - prefix[j] >= A) j++;
while(prefix[i] - prefix[k] > B) k++;
ll t = prefix[i] - prefix[j - 1];
minimize(ans, abs(t - A) + abs(t - B));
t = prefix[i] - prefix[k];
minimize(ans, abs(t - A) + abs(t - B));
}
}
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("DAUTU.inp","r",stdin);
// freopen("DAUTU.out","w",stdout);
input();
solve();
// return subtask4::solve(), 0;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |