// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 500 + 5;
const int inf = 1e18 + 7;
int n, m, s, t, table[maxn][maxn], a[maxn];
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> s >> t;
if(s > t) swap(s, t);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin >> table[i][j];
int ans = inf;
for(int u = 0; u < n; u++){
for(int i = 0; i < m; i++) a[i] = 0;
for(int d = u; d < n; d++){
for(int i = 0; i < m; i++) a[i] += table[d][i];
int r = 0, sm = 0;
for(int i = 0; i < m; i++){
while(r <= i || (r < m && a[r] + sm <= t)) sm += a[r++];
smin(ans, abs(s - sm) + abs(t - sm));
if(r < m) smin(ans, abs(s - (sm + a[r])) + abs(t - (sm + a[r])));
sm -= a[i];
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
880 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
856 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
856 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
880 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
856 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
856 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
23 |
Correct |
250 ms |
4840 KB |
Output is correct |
24 |
Correct |
252 ms |
4840 KB |
Output is correct |
25 |
Correct |
246 ms |
4836 KB |
Output is correct |
26 |
Correct |
258 ms |
4692 KB |
Output is correct |
27 |
Correct |
164 ms |
3848 KB |
Output is correct |
28 |
Correct |
252 ms |
4944 KB |
Output is correct |
29 |
Correct |
256 ms |
4844 KB |
Output is correct |
30 |
Correct |
247 ms |
4688 KB |
Output is correct |
31 |
Correct |
33 ms |
2396 KB |
Output is correct |
32 |
Correct |
246 ms |
4948 KB |
Output is correct |
33 |
Correct |
248 ms |
4840 KB |
Output is correct |
34 |
Correct |
246 ms |
4792 KB |
Output is correct |
35 |
Correct |
248 ms |
4840 KB |
Output is correct |
36 |
Correct |
246 ms |
4688 KB |
Output is correct |
37 |
Correct |
252 ms |
4844 KB |
Output is correct |
38 |
Correct |
247 ms |
4688 KB |
Output is correct |
39 |
Correct |
247 ms |
4700 KB |
Output is correct |
40 |
Correct |
247 ms |
4836 KB |
Output is correct |
41 |
Correct |
182 ms |
3392 KB |
Output is correct |
42 |
Correct |
179 ms |
3420 KB |
Output is correct |
43 |
Correct |
184 ms |
3576 KB |
Output is correct |
44 |
Correct |
177 ms |
3328 KB |
Output is correct |