#pragma GCC optimize("O4,unroll-loops")
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define endl "\n"
#define mp make_pair
#define out(x) cout << #x << " = " << x << endl
int n,m;
ll arr[505][505],pre[505][505],a,b;
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
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>>arr[i][j], pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + arr[i][j];
}
if (n*m <= 10000){
ll ans = 1e18;
for (int i=1;i<=n;i++) for (int j=i;j<=n;j++){
for (int k=1;k<=m;k++) for (int l=k;l<=m;l++){
ll x = pre[j][l] - pre[j][k-1] - pre[i-1][l] + pre[i-1][k-1];
ans = min(ans, abs(a-x) + abs(b-x));
}
}
cout<<ans<<endl;
return 0;
}
ll ans = 1e18;
for (int i=1;i<=n;i++) for (int j=i;j<=n;j++){
ll tmp[505]; tmp[0] = 0;
for (int k=1;k<=m;k++) tmp[k] = pre[j][k] - pre[j][k-1] - pre[i-1][k] + pre[i-1][k-1];
int r = 0; ll sum = 0;
for (int i=1;i<=m;i++){
while(1){
ans = min(ans, abs(sum-a) + abs(sum-b));
if (r==m) break;
if (sum >= a) break;
r++; sum += tmp[r];
}
ans = min(ans, abs(sum-a) + abs(sum-b));
sum -= tmp[i];
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
852 KB |
Output is correct |
11 |
Correct |
52 ms |
1108 KB |
Output is correct |
12 |
Correct |
52 ms |
1108 KB |
Output is correct |
13 |
Correct |
56 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
62 ms |
1112 KB |
Output is correct |
16 |
Correct |
65 ms |
1112 KB |
Output is correct |
17 |
Correct |
55 ms |
1112 KB |
Output is correct |
18 |
Correct |
52 ms |
1108 KB |
Output is correct |
19 |
Correct |
53 ms |
1112 KB |
Output is correct |
20 |
Correct |
51 ms |
1228 KB |
Output is correct |
21 |
Correct |
51 ms |
1108 KB |
Output is correct |
22 |
Correct |
53 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
14 ms |
852 KB |
Output is correct |
11 |
Correct |
52 ms |
1108 KB |
Output is correct |
12 |
Correct |
52 ms |
1108 KB |
Output is correct |
13 |
Correct |
56 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
62 ms |
1112 KB |
Output is correct |
16 |
Correct |
65 ms |
1112 KB |
Output is correct |
17 |
Correct |
55 ms |
1112 KB |
Output is correct |
18 |
Correct |
52 ms |
1108 KB |
Output is correct |
19 |
Correct |
53 ms |
1112 KB |
Output is correct |
20 |
Correct |
51 ms |
1228 KB |
Output is correct |
21 |
Correct |
51 ms |
1108 KB |
Output is correct |
22 |
Correct |
53 ms |
1108 KB |
Output is correct |
23 |
Correct |
355 ms |
4260 KB |
Output is correct |
24 |
Correct |
357 ms |
4204 KB |
Output is correct |
25 |
Correct |
361 ms |
4180 KB |
Output is correct |
26 |
Correct |
378 ms |
4300 KB |
Output is correct |
27 |
Correct |
242 ms |
4292 KB |
Output is correct |
28 |
Correct |
355 ms |
4264 KB |
Output is correct |
29 |
Correct |
359 ms |
4204 KB |
Output is correct |
30 |
Correct |
378 ms |
4180 KB |
Output is correct |
31 |
Correct |
49 ms |
3796 KB |
Output is correct |
32 |
Correct |
366 ms |
4264 KB |
Output is correct |
33 |
Correct |
358 ms |
4260 KB |
Output is correct |
34 |
Correct |
374 ms |
4260 KB |
Output is correct |
35 |
Correct |
362 ms |
4324 KB |
Output is correct |
36 |
Correct |
370 ms |
4396 KB |
Output is correct |
37 |
Correct |
356 ms |
4388 KB |
Output is correct |
38 |
Correct |
382 ms |
4392 KB |
Output is correct |
39 |
Correct |
354 ms |
4388 KB |
Output is correct |
40 |
Correct |
380 ms |
4392 KB |
Output is correct |
41 |
Correct |
324 ms |
4300 KB |
Output is correct |
42 |
Correct |
335 ms |
4428 KB |
Output is correct |
43 |
Correct |
315 ms |
4424 KB |
Output is correct |
44 |
Correct |
322 ms |
4424 KB |
Output is correct |