# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define pii pair <int, int>
# define pll pair <ll, ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 125;
const ll maxl = 20 + 0;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
using namespace std;
ll n, m, l, r;
ll a[525][525];
ll p[525], pr[525][525];
void ma1n ()
{
cin >> n >> m >> l >> r;
if (l > r) swap(l, r);
for (ll i = 1; i <= n; ++i)
{
for (ll j = 1; j <= m; ++j)
{
cin >> a[i][j];
pr[i][j] = pr[i][j - 1] + a[i][j];
}
}
ll ans = inf;
for (ll i = 1; i <= m; ++i)
{
for (ll j = i; j <= m; ++j)
{
for (ll ij = 1; ij <= n; ++ij)
{
p[ij] = p[ij - 1] + pr[ij][j] - pr[ij][i - 1];
}
p[n + 1] = p[n + 2] = p[n];
ll cur = 0;
for (ll ij = 1; ij <= n; ++ij)
{
while (p[ij] - p[cur] > r) ++cur;
ans = min(ans, abs(p[ij] - p[cur] - l) + abs(p[ij] - p[cur] - r));
if (cur > 0) ans = min(ans, abs(p[ij] - p[cur - 1] - l) + abs(p[ij] - p[cur - 1] - r));
}
}
}
cout << ans << nl;
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; test++)
{
// cout << "Case " << test << ":" << nl;
ma1n();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
1236 KB |
Output is correct |
12 |
Correct |
5 ms |
1236 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
1236 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
6 ms |
1236 KB |
Output is correct |
18 |
Correct |
4 ms |
1180 KB |
Output is correct |
19 |
Correct |
4 ms |
1108 KB |
Output is correct |
20 |
Correct |
4 ms |
1108 KB |
Output is correct |
21 |
Correct |
4 ms |
1104 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
1236 KB |
Output is correct |
12 |
Correct |
5 ms |
1236 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
1236 KB |
Output is correct |
16 |
Correct |
5 ms |
1236 KB |
Output is correct |
17 |
Correct |
6 ms |
1236 KB |
Output is correct |
18 |
Correct |
4 ms |
1180 KB |
Output is correct |
19 |
Correct |
4 ms |
1108 KB |
Output is correct |
20 |
Correct |
4 ms |
1108 KB |
Output is correct |
21 |
Correct |
4 ms |
1104 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
23 |
Correct |
429 ms |
6852 KB |
Output is correct |
24 |
Correct |
450 ms |
6892 KB |
Output is correct |
25 |
Correct |
427 ms |
6852 KB |
Output is correct |
26 |
Correct |
505 ms |
6856 KB |
Output is correct |
27 |
Correct |
233 ms |
6092 KB |
Output is correct |
28 |
Correct |
436 ms |
6900 KB |
Output is correct |
29 |
Correct |
428 ms |
6852 KB |
Output is correct |
30 |
Correct |
453 ms |
6856 KB |
Output is correct |
31 |
Correct |
15 ms |
4436 KB |
Output is correct |
32 |
Correct |
423 ms |
6852 KB |
Output is correct |
33 |
Correct |
441 ms |
6932 KB |
Output is correct |
34 |
Correct |
439 ms |
6884 KB |
Output is correct |
35 |
Correct |
449 ms |
6848 KB |
Output is correct |
36 |
Correct |
452 ms |
6852 KB |
Output is correct |
37 |
Correct |
452 ms |
6852 KB |
Output is correct |
38 |
Correct |
431 ms |
6860 KB |
Output is correct |
39 |
Correct |
447 ms |
6848 KB |
Output is correct |
40 |
Correct |
455 ms |
6916 KB |
Output is correct |
41 |
Correct |
303 ms |
5588 KB |
Output is correct |
42 |
Correct |
305 ms |
5588 KB |
Output is correct |
43 |
Correct |
306 ms |
5592 KB |
Output is correct |
44 |
Correct |
320 ms |
5588 KB |
Output is correct |