#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, a, b, c[501][501], price[501][501];
cin >> n >> m >> a >> b;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> c[i][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
price[i][j] = price[i - 1][j] + price[i][j - 1] - price[i - 1][j - 1] + c[i][j];
}
}
long long ans = INT_MAX;
if(a > b)
{
swap(a, b);
}
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
long long p = 0;
for(int k = 1; k <= m; k++)
{
long long val = price[j][k]-price[i][k]-price[j][0]+price[i][0];
long long val2 = price[j][p] - price[i-1][p] - price[j][0] + price[i-1][0];
while(p < k && val - val2 >= a)
{
p++;
}
if(p < k)
{
ans = min(ans, abs(val - val2 - a) + abs(val - val2 - b));
}
if(p >= 1)
{
long long val3 = price[j][p-1] - price[i-1][p-1] - price[j][0] + price[i-1][0];
ans = min(ans, abs(val - val3 - a) + abs(val - val3 - b));
}
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |