#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
const int MAXN = 505;
int N, M, A, B;
long long a[MAXN][MAXN], F[MAXN];
long long get(int x, int y, int u, int v) {
return a[u][v] - a[x - 1][v] - a[u][y - 1] + a[x - 1][y - 1];
}
void you_make_it(void) {
cin >> N >> M >> A >> B; if(A > B) swap(A, B);
FOR(i, 1, N) FOR(j, 1, M) {
cin >> a[i][j];
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
long long ans = 1e18;
FOR(l, 1, N) FOR(r, l, N) {
FOR(i, 1, M) F[i] = get(l, i, r, i);
int j = 0;
long long sum = 0;
FOR(i, 1, M) {
while(j < M && abs(sum - A) + abs(sum - B) > abs(sum - A + F[j + 1]) + abs(sum - B + F[j + 1])) {
sum += F[++j];
}
minimize(ans, abs(sum - A) + abs(sum - B));
sum -= F[i];
}
}
cout << ans;
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
856 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
856 KB |
Output is correct |
18 |
Correct |
3 ms |
856 KB |
Output is correct |
19 |
Correct |
3 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
856 KB |
Output is correct |
21 |
Correct |
3 ms |
1112 KB |
Output is correct |
22 |
Correct |
3 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
856 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
856 KB |
Output is correct |
18 |
Correct |
3 ms |
856 KB |
Output is correct |
19 |
Correct |
3 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
856 KB |
Output is correct |
21 |
Correct |
3 ms |
1112 KB |
Output is correct |
22 |
Correct |
3 ms |
856 KB |
Output is correct |
23 |
Correct |
398 ms |
4944 KB |
Output is correct |
24 |
Correct |
389 ms |
4840 KB |
Output is correct |
25 |
Correct |
388 ms |
4688 KB |
Output is correct |
26 |
Correct |
400 ms |
4944 KB |
Output is correct |
27 |
Correct |
259 ms |
3920 KB |
Output is correct |
28 |
Correct |
388 ms |
4688 KB |
Output is correct |
29 |
Correct |
395 ms |
4840 KB |
Output is correct |
30 |
Correct |
389 ms |
4688 KB |
Output is correct |
31 |
Correct |
52 ms |
2392 KB |
Output is correct |
32 |
Correct |
387 ms |
4844 KB |
Output is correct |
33 |
Correct |
386 ms |
4688 KB |
Output is correct |
34 |
Correct |
393 ms |
4944 KB |
Output is correct |
35 |
Correct |
395 ms |
4944 KB |
Output is correct |
36 |
Correct |
426 ms |
4844 KB |
Output is correct |
37 |
Correct |
388 ms |
4848 KB |
Output is correct |
38 |
Correct |
390 ms |
4688 KB |
Output is correct |
39 |
Correct |
405 ms |
4944 KB |
Output is correct |
40 |
Correct |
395 ms |
4944 KB |
Output is correct |
41 |
Correct |
283 ms |
3576 KB |
Output is correct |
42 |
Correct |
289 ms |
3576 KB |
Output is correct |
43 |
Correct |
284 ms |
3416 KB |
Output is correct |
44 |
Correct |
294 ms |
3576 KB |
Output is correct |