#include <bits/stdc++.h>
using namespace std;
#define inf 1e18
#define int long long
const int mod=1e9+7;
const int N=501;
int mat[N][N], dp[N][N];
inline int toplam(int x1,int y1,int x2,int y2){
return dp[y2][x2]-dp[y1-1][x2]-dp[y2][x1-1]+dp[y1-1][x1-1];
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int x,y,a,b;
cin>>x>>y>>a>>b;
if(a>b)
swap(a,b);
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++){
cin>>mat[i][j];
dp[i][j]=mat[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
int ans=inf;
for(int i=1;i<=x;i++)
for(int j=1;j<=x;j++){
int l=1,r=1;
while(r<=y){
int suan=toplam(i,l,j,r);
ans=min(ans,abs(a-suan)+abs(b-suan));
if(suan>b){
l++;
if(l>r)
r++;
}
else if(suan>=a){
cout<<b-a<<"\n";
return 0;
}
else
r++;
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |