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>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define pii pair <int, int>
# define pll pair <ll, ll>
  
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
  
const ll maxn = 1e5 + 125;
const ll maxl = 20 + 0;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
using namespace std;
ll n, m, l, r;
ll a[525][525];
ll p[525], pr[525][525];
 
void ma1n ()
{
	cin >> n >> m >> l >> r;
	if (l > r) swap(l, r);
	for (ll i = 1; i <= n; ++i)
	{
		for (ll j = 1; j <= m; ++j)
		{
			cin >> a[i][j];
			pr[i][j] = pr[i][j - 1] + a[i][j];
		}
	}
	ll ans = inf;
	for (ll i = 1; i <= m; ++i)
	{
		for (ll j = i; j <= m; ++j)
		{
			for (ll ij = 1; ij <= n; ++ij)
			{
				p[ij] = p[ij - 1] + pr[ij][j] - pr[ij][i - 1];
			}
			p[n + 1]  = p[n + 2] = p[n];
			ll cur = 0;
			for (ll ij = 1; ij <= n; ++ij)
			{
				while (p[ij] - p[cur] > r) ++cur;
				ans = min(ans, abs(p[ij] - p[cur] - l) + abs(p[ij] - p[cur] - r));
				if (cur > 0) ans = min(ans, abs(p[ij] - p[cur - 1] - l) + abs(p[ij] - p[cur - 1] - r));
			}
		}
	}
	cout << ans << nl;
}
    
int main ()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int ttt = 1;
//  cin >> ttt;
    for (int test = 1; test <= ttt; test++)
    {
//      cout << "Case " << test << ":" << nl;
        ma1n();
    }
    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... |