#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 |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
3 ms |
2640 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2640 KB |
Output is correct |
13 |
Correct |
5 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
5 ms |
2640 KB |
Output is correct |
16 |
Correct |
5 ms |
2640 KB |
Output is correct |
17 |
Correct |
5 ms |
2896 KB |
Output is correct |
18 |
Correct |
4 ms |
2640 KB |
Output is correct |
19 |
Correct |
4 ms |
2640 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
4 ms |
2640 KB |
Output is correct |
22 |
Correct |
4 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
3 ms |
2640 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2640 KB |
Output is correct |
13 |
Correct |
5 ms |
2640 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
5 ms |
2640 KB |
Output is correct |
16 |
Correct |
5 ms |
2640 KB |
Output is correct |
17 |
Correct |
5 ms |
2896 KB |
Output is correct |
18 |
Correct |
4 ms |
2640 KB |
Output is correct |
19 |
Correct |
4 ms |
2640 KB |
Output is correct |
20 |
Correct |
4 ms |
2896 KB |
Output is correct |
21 |
Correct |
4 ms |
2640 KB |
Output is correct |
22 |
Correct |
4 ms |
2640 KB |
Output is correct |
23 |
Correct |
335 ms |
4408 KB |
Output is correct |
24 |
Correct |
352 ms |
4176 KB |
Output is correct |
25 |
Correct |
307 ms |
5928 KB |
Output is correct |
26 |
Correct |
315 ms |
6716 KB |
Output is correct |
27 |
Correct |
207 ms |
4388 KB |
Output is correct |
28 |
Correct |
313 ms |
4176 KB |
Output is correct |
29 |
Correct |
305 ms |
5448 KB |
Output is correct |
30 |
Correct |
305 ms |
4176 KB |
Output is correct |
31 |
Correct |
45 ms |
4176 KB |
Output is correct |
32 |
Correct |
306 ms |
4176 KB |
Output is correct |
33 |
Correct |
304 ms |
4176 KB |
Output is correct |
34 |
Correct |
338 ms |
4428 KB |
Output is correct |
35 |
Correct |
312 ms |
4520 KB |
Output is correct |
36 |
Correct |
324 ms |
4176 KB |
Output is correct |
37 |
Correct |
310 ms |
4168 KB |
Output is correct |
38 |
Correct |
367 ms |
5192 KB |
Output is correct |
39 |
Correct |
343 ms |
6208 KB |
Output is correct |
40 |
Correct |
326 ms |
4176 KB |
Output is correct |
41 |
Correct |
289 ms |
4176 KB |
Output is correct |
42 |
Correct |
280 ms |
5572 KB |
Output is correct |
43 |
Correct |
277 ms |
4412 KB |
Output is correct |
44 |
Correct |
278 ms |
4408 KB |
Output is correct |