#include <iostream>
#include <algorithm>
using namespace std;
#define int int64_t
#define minimize(a,b) a=min(a,b)
int64_t ans = 1e18;
int n, m, a, b;
int x;
int64_t ps[503][503];
int64_t s[503];
int64_t toQ(int64_t x){
// cout << x << '\n';
return abs(x-a) + abs(x-b);
}
signed main()
{
cin.tie(0) -> sync_with_stdio(0);
// if(fopen("DAUTU.inp", "r")){
// freopen("DAUTU.inp", "r",stdin);
// freopen("DAUTU.out", "w",stdout);
// }
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 >> x;
ps[i][j] = ps[i][j-1]+x;
}
// cout << ps[3][1] << '\n';
for(int l=1; l<=m; ++l)
for(int r=l; r<=m; ++r){
for(int i=1; i<=n; ++i) s[i] = ps[i][r]-ps[i][l-1];
int64_t sum=0;
// cout << l << ' ' << r << "*\n";
for(int i=1, j=1; i<=n; ++i){
sum += s[i];
// cout << sum << ' ';
while(sum>b){
minimize(ans, toQ(sum));
sum-=s[j++];
}
minimize(ans, toQ(sum));
}
// cout << '\n';
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
848 KB |
Output is correct |
12 |
Correct |
4 ms |
848 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
848 KB |
Output is correct |
16 |
Correct |
4 ms |
848 KB |
Output is correct |
17 |
Correct |
5 ms |
848 KB |
Output is correct |
18 |
Correct |
3 ms |
1016 KB |
Output is correct |
19 |
Correct |
2 ms |
632 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
1016 KB |
Output is correct |
22 |
Correct |
3 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
848 KB |
Output is correct |
12 |
Correct |
4 ms |
848 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
4 ms |
848 KB |
Output is correct |
16 |
Correct |
4 ms |
848 KB |
Output is correct |
17 |
Correct |
5 ms |
848 KB |
Output is correct |
18 |
Correct |
3 ms |
1016 KB |
Output is correct |
19 |
Correct |
2 ms |
632 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
1016 KB |
Output is correct |
22 |
Correct |
3 ms |
848 KB |
Output is correct |
23 |
Correct |
259 ms |
2656 KB |
Output is correct |
24 |
Correct |
275 ms |
2384 KB |
Output is correct |
25 |
Correct |
275 ms |
2424 KB |
Output is correct |
26 |
Correct |
261 ms |
4428 KB |
Output is correct |
27 |
Correct |
125 ms |
2384 KB |
Output is correct |
28 |
Correct |
260 ms |
2396 KB |
Output is correct |
29 |
Correct |
260 ms |
2384 KB |
Output is correct |
30 |
Correct |
270 ms |
2400 KB |
Output is correct |
31 |
Correct |
10 ms |
2128 KB |
Output is correct |
32 |
Correct |
280 ms |
2316 KB |
Output is correct |
33 |
Correct |
264 ms |
2376 KB |
Output is correct |
34 |
Correct |
277 ms |
2384 KB |
Output is correct |
35 |
Correct |
272 ms |
2376 KB |
Output is correct |
36 |
Correct |
272 ms |
2384 KB |
Output is correct |
37 |
Correct |
274 ms |
2384 KB |
Output is correct |
38 |
Correct |
272 ms |
2384 KB |
Output is correct |
39 |
Correct |
276 ms |
4072 KB |
Output is correct |
40 |
Correct |
266 ms |
2384 KB |
Output is correct |
41 |
Correct |
200 ms |
2384 KB |
Output is correct |
42 |
Correct |
211 ms |
2384 KB |
Output is correct |
43 |
Correct |
196 ms |
2384 KB |
Output is correct |
44 |
Correct |
204 ms |
2384 KB |
Output is correct |