#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 615;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"
ll n, m, a, b, t[MAX][MAX], ps[MAX][MAX];
ll query(int x, int y, int u, int v){
return ps[u][v] - ps[x - 1][v] - ps[u][y - 1] + ps[x - 1][y - 1];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> a >> b;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> t[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + t[i][j];
}
}
ll ans = INF;
if(a > b) swap(a, b);
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
ll p = 0;
for(int k = 1; k <= m; k++){
ll val = query(i, 1, j, k);
while(p < k && val - query(i, 1, j, p) >= a) p++;
if(p < k) ans = min(ans, abs(val - query(i, 1, j, p) - a) + abs(val - query(i, 1, j, p) - b));
if(p >= 1) ans = min(ans, abs(val - query(i, 1, j, p - 1) - a) + abs(val - query(i, 1, j, p - 1) - b));
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
980 KB |
Output is correct |
11 |
Correct |
4 ms |
1364 KB |
Output is correct |
12 |
Correct |
4 ms |
1308 KB |
Output is correct |
13 |
Correct |
6 ms |
1368 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
4 ms |
1364 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
5 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1232 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
3 ms |
1244 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
980 KB |
Output is correct |
11 |
Correct |
4 ms |
1364 KB |
Output is correct |
12 |
Correct |
4 ms |
1308 KB |
Output is correct |
13 |
Correct |
6 ms |
1368 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
4 ms |
1364 KB |
Output is correct |
16 |
Correct |
4 ms |
1364 KB |
Output is correct |
17 |
Correct |
5 ms |
1364 KB |
Output is correct |
18 |
Correct |
3 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1232 KB |
Output is correct |
20 |
Correct |
3 ms |
1236 KB |
Output is correct |
21 |
Correct |
3 ms |
1244 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
276 ms |
7552 KB |
Output is correct |
24 |
Correct |
341 ms |
7548 KB |
Output is correct |
25 |
Correct |
281 ms |
7560 KB |
Output is correct |
26 |
Correct |
289 ms |
7500 KB |
Output is correct |
27 |
Correct |
190 ms |
6688 KB |
Output is correct |
28 |
Correct |
278 ms |
7448 KB |
Output is correct |
29 |
Correct |
282 ms |
7552 KB |
Output is correct |
30 |
Correct |
281 ms |
7576 KB |
Output is correct |
31 |
Correct |
48 ms |
4820 KB |
Output is correct |
32 |
Correct |
286 ms |
7552 KB |
Output is correct |
33 |
Correct |
280 ms |
7552 KB |
Output is correct |
34 |
Correct |
306 ms |
7500 KB |
Output is correct |
35 |
Correct |
281 ms |
7552 KB |
Output is correct |
36 |
Correct |
280 ms |
7628 KB |
Output is correct |
37 |
Correct |
272 ms |
7552 KB |
Output is correct |
38 |
Correct |
277 ms |
7548 KB |
Output is correct |
39 |
Correct |
298 ms |
7552 KB |
Output is correct |
40 |
Correct |
280 ms |
7552 KB |
Output is correct |
41 |
Correct |
174 ms |
6300 KB |
Output is correct |
42 |
Correct |
183 ms |
6288 KB |
Output is correct |
43 |
Correct |
193 ms |
6288 KB |
Output is correct |
44 |
Correct |
164 ms |
6288 KB |
Output is correct |