#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
vector<vector<ll>> v;
ll getSum(int i,int l,int r){
return v[i][r] - (l ? v[i][l-1] : 0);
}
void solve(){
int n,m,a,b;
cin>>n>>m>>a>>b;
v.resize(n,vector<ll>(m));
for(auto &i:v) for(auto &j:i) cin>>j;
for(int i=0;i<n;i++){
for(int j=1;j<m;j++) v[i][j] += v[i][j-1];
}
if(a > b) swap(a,b);
ll ans = 1e18;
for(int l=0;l<m;l++){
for(int r=l;r<m;r++){
vector<ll> prefix{0};
for(int i=0;i<n;i++) prefix.push_back(getSum(i,l,r));
for(int i=1;i<prefix.size();i++) prefix[i] += prefix[i-1];
int j=0;
ll ans2 = 0;
for(int i=1;i<=n;i++){
if(i>1 && j) ans2 += getSum(i-1,l,r);
while(j<i&&prefix[i]-prefix[j]>b) ans2 = prefix[i]-prefix[j] , j ++;
ll ans1 = j != i ? prefix[i] - prefix[j] : 1e18;
if(j) ans = min(ans , abs(ans2 - a) + abs(ans2 - b));
ans = min(abs(ans1-a)+abs(ans1-b) , ans);
}
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:23:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=1;i<prefix.size();i++) prefix[i] += prefix[i-1];
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
608 KB |
Output is correct |
12 |
Correct |
6 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
6 ms |
608 KB |
Output is correct |
16 |
Correct |
7 ms |
608 KB |
Output is correct |
17 |
Correct |
6 ms |
608 KB |
Output is correct |
18 |
Correct |
5 ms |
592 KB |
Output is correct |
19 |
Correct |
5 ms |
356 KB |
Output is correct |
20 |
Correct |
5 ms |
588 KB |
Output is correct |
21 |
Correct |
5 ms |
596 KB |
Output is correct |
22 |
Correct |
5 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
608 KB |
Output is correct |
12 |
Correct |
6 ms |
468 KB |
Output is correct |
13 |
Correct |
6 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
6 ms |
608 KB |
Output is correct |
16 |
Correct |
7 ms |
608 KB |
Output is correct |
17 |
Correct |
6 ms |
608 KB |
Output is correct |
18 |
Correct |
5 ms |
592 KB |
Output is correct |
19 |
Correct |
5 ms |
356 KB |
Output is correct |
20 |
Correct |
5 ms |
588 KB |
Output is correct |
21 |
Correct |
5 ms |
596 KB |
Output is correct |
22 |
Correct |
5 ms |
356 KB |
Output is correct |
23 |
Correct |
544 ms |
4980 KB |
Output is correct |
24 |
Correct |
591 ms |
4844 KB |
Output is correct |
25 |
Correct |
547 ms |
4716 KB |
Output is correct |
26 |
Correct |
551 ms |
4700 KB |
Output is correct |
27 |
Correct |
251 ms |
3372 KB |
Output is correct |
28 |
Correct |
551 ms |
4832 KB |
Output is correct |
29 |
Correct |
540 ms |
4596 KB |
Output is correct |
30 |
Correct |
540 ms |
4688 KB |
Output is correct |
31 |
Correct |
15 ms |
860 KB |
Output is correct |
32 |
Correct |
546 ms |
4836 KB |
Output is correct |
33 |
Correct |
544 ms |
4836 KB |
Output is correct |
34 |
Correct |
544 ms |
4696 KB |
Output is correct |
35 |
Correct |
562 ms |
4836 KB |
Output is correct |
36 |
Correct |
545 ms |
4972 KB |
Output is correct |
37 |
Correct |
549 ms |
4948 KB |
Output is correct |
38 |
Correct |
583 ms |
4688 KB |
Output is correct |
39 |
Correct |
546 ms |
4832 KB |
Output is correct |
40 |
Correct |
552 ms |
4840 KB |
Output is correct |
41 |
Correct |
480 ms |
3576 KB |
Output is correct |
42 |
Correct |
469 ms |
3576 KB |
Output is correct |
43 |
Correct |
463 ms |
3420 KB |
Output is correct |
44 |
Correct |
470 ms |
3576 KB |
Output is correct |