#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <set>
#include <stack>
#include <bitset>
#define dbug(x) cout << (#x) << " -> " << x << endl
#define ll long long
#define INF (1e9)
#define MAX (int) (505)
#define MOD 1000000007
#define par pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(x) (int) ((x).size())
#define esq(x) (x<<1)
#define dir(x) ((x<<1)|1)
#define lsb(x) (x & -x)
#define W(x) cout << #x << ": " << x << endl
#define Wii(x) cout << x.first << ' ' << x.second << endl
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll n, m, a, b, tab[MAX][MAX], psa[MAX][MAX], sum, ans;
int main()
{_
cin >> n >> m >> a >> b;
if (a < b) swap(a, b);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> tab[i][j];
psa[i][j] = psa[i-1][j] + tab[i][j];
}
}
/*
ini -> 1 1 1
1 1 1
fim -> 1 1 1
1 1 1
2 2 2
3 3 3
psa[i][j] =
psa[r][fim] - psa[r][ini-1];
*/
ans = INF;
for (int ini = 1; ini <= n; ini++)
{
for (int fim = ini; fim <= n; fim++)
{
int l = 1, r = 1;
sum = psa[fim][1] - psa[ini-1][1];
while (r <= m)
{
ans = min(ans, abs(sum-a) + abs(sum-b));
while (sum > a && l <= r)
{
sum -= psa[fim][l] - psa[ini-1][l], l++;
ans = min(ans, abs(sum-a) + abs(sum-b));
}
r++;
sum += psa[fim][r] - psa[ini-1][r];
ans = min(ans, abs(sum-a) + abs(sum-b));
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
1108 KB |
Output is correct |
12 |
Correct |
6 ms |
1108 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
1108 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1108 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
1108 KB |
Output is correct |
12 |
Correct |
6 ms |
1108 KB |
Output is correct |
13 |
Correct |
5 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
1108 KB |
Output is correct |
16 |
Correct |
5 ms |
1108 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1108 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
446 ms |
4264 KB |
Output is correct |
24 |
Correct |
429 ms |
4280 KB |
Output is correct |
25 |
Correct |
465 ms |
4180 KB |
Output is correct |
26 |
Correct |
439 ms |
4268 KB |
Output is correct |
27 |
Correct |
298 ms |
4220 KB |
Output is correct |
28 |
Correct |
426 ms |
4264 KB |
Output is correct |
29 |
Correct |
433 ms |
4280 KB |
Output is correct |
30 |
Correct |
422 ms |
4152 KB |
Output is correct |
31 |
Correct |
58 ms |
3884 KB |
Output is correct |
32 |
Correct |
448 ms |
4288 KB |
Output is correct |
33 |
Correct |
426 ms |
4264 KB |
Output is correct |
34 |
Correct |
455 ms |
4300 KB |
Output is correct |
35 |
Correct |
446 ms |
4268 KB |
Output is correct |
36 |
Correct |
439 ms |
4268 KB |
Output is correct |
37 |
Correct |
453 ms |
4264 KB |
Output is correct |
38 |
Correct |
428 ms |
4264 KB |
Output is correct |
39 |
Correct |
457 ms |
4264 KB |
Output is correct |
40 |
Correct |
432 ms |
4268 KB |
Output is correct |
41 |
Correct |
218 ms |
4172 KB |
Output is correct |
42 |
Correct |
212 ms |
4260 KB |
Output is correct |
43 |
Correct |
211 ms |
4264 KB |
Output is correct |
44 |
Correct |
217 ms |
4260 KB |
Output is correct |