Submission #1274050

#TimeUsernameProblemLanguageResultExecution timeMemory
1274050bayengZemljište (COCI22_zemljiste)C++20
0 / 70
1 ms568 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
    ll n, m, a, b, x, y;
    cin >> n >> m >> a >> b;
    x = min(a,b), y = max(a,b);
    ll p[n+2][m+2], prefs[n+2][m+2];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> p[i][j];
        }
    }
    for(int i=1;i<=m;i++){prefs[0][i] = 0;}
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            prefs[i][j] = prefs[i-1][j] + p[i][j];
            // cout << prefs[i][j] << " ";
        }
        // cout << endl;
    }
    ll ans = 1e18;
    for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
            ll ar[m+2], l = 1, r = 1, tmp = 0;
            for(int k=1;k<=m;k++){
                ar[k] = prefs[j][k]-prefs[i-1][k];
            }
            while(r<=m){
                tmp += ar[r];
                r++;
                if(tmp>y&&l<r){
                    tmp -= ar[l];
                    l++;
                }
                ll sum = abs(tmp-x)+abs(tmp-y);
                ans = min(ans, sum);
            }
        }
    }
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...