This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 505;
lg v[N][N];
lg get_sum(lg a, lg b, lg c, lg d)
{
if(c > d) swap(c, d);
if(a > b) swap(a, b);
return v[b][d]-v[a-1][d]-v[b][c-1]+v[a-1][c-1];
}
int main()
{
fastio;
lg n, m, a, b;
cin >> n >> m >> a >> b;
if(b < a) swap(a, b);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> v[i][j];
v[i][j] += v[i-1][j]+v[i][j-1]-v[i-1][j-1];
}
}
lg ans = 2e18;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
lg ptr1 = m, ptr2 = 1;
for(int k = 1; k <= m; k++)
{
while(ptr2 < k && get_sum(j, i, k, ptr2+1) >= a) ptr2++;
lg y = get_sum(j, i, k, ptr2);
ans = min(ans, abs(y-a)+abs(y-b));
}
for(int k = m; k; k--)
{
while(ptr1 > k && get_sum(j, i, k, ptr1) > b) ptr1--;
lg y = get_sum(j, i, k, ptr1);
ans = min(ans, abs(y-a)+abs(y-b));
}
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |