#warning Check FastIO
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <random>
#include <chrono>
#include <iomanip>
#include <vector>
#include <fstream>
using namespace std;
#define vll vector<ll>
#define sll set<ll>
#define vstr vector<string>
#define ll long long
#define ld long double
#define supra main
#define pb push_back
#define add insert
#define rall(x) rbegin(x),rend(x)
#define all(x) (x).begin(),(x).end()
#define I ios_base::sync_with_stdio(false);
#define Hear cin.tie(NULL);
#define Shots cout.tie(NULL);
#define Ratatatata
#define bits_on(a) (__builtin_popcountll(a))
#define mx_pw2(a) (__builtin_ctzll(a))
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUFFLE(v) shuffle(all(v), RNG);
const ll inf=LLONG_MAX;
void input(vll& a)
{
for(auto& i:a)
cin>>i;
}
void pyn(bool a)
{
cout<<(a?"YES":"NO")<<endl;
}
ll powmod(ll a,ll b,ll modulo)
{
if(b==0){
return 1;
}
ll temp=powmod(a,b/2,modulo);
if(b%2==0){
return (temp*temp)%modulo;
}
else{
return (a*((temp*temp)%modulo))%modulo;
}
}
bool Prime(ll n){
for (ll i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return (n>1);
}
const int N=502;
ll a[N][N],pre[N][N];
void solve()
{
ll n,m,ax,bx;
cin>>n>>m>>ax>>bx;
if(ax>bx)
swap(ax,bx);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
pre[i][j]=pre[i][j-1]+a[i][j];
}
}
ll ans=ax+bx;
for(int x=1;x<=m;x++)
{
for(int xp=x;xp<=m;xp++)
{
vll pref={0};
int i=0;
for(int row=1;row<=n;row++)
{
pref.pb(pref.back()+pre[row][xp]-pre[row][x-1]);
ll val=pref.back()-bx;
while(i<pref.size() and pref[i]<val)
i++;
if(i<pref.size())
ans=min(ans,abs(ax-pref.back()+pref[i])+abs(bx-pref.back()+pref[i]));
if(i>0)
ans=min(ans,abs(ax-pref.back()+pref[i-1])+abs(bx-pref.back()+pref[i-1]));
}
}
}
cout<<ans<<'\n';
}
int supra(){
I Hear Shots Ratatatata
ll tqwertyuiop=1;
for(int tp=1;tp<=tqwertyuiop;tp++)
{
// cout<<"Case #"<<tp<<": ";
solve();
}
return 0;
}
/*
Bro use only in a emergency
this is kind of hacking
*/
Compilation message
Main.cpp:1:2: warning: #warning Check FastIO [-Wcpp]
1 | #warning Check FastIO
| ^~~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:94:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | while(i<pref.size() and pref[i]<val)
| ~^~~~~~~~~~~~
Main.cpp:96:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | if(i<pref.size())
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
1020 KB |
Output is correct |
12 |
Correct |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
6 ms |
1124 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
9 ms |
1260 KB |
Output is correct |
18 |
Correct |
3 ms |
1112 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1264 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
1020 KB |
Output is correct |
12 |
Correct |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
6 ms |
1124 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
1116 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
9 ms |
1260 KB |
Output is correct |
18 |
Correct |
3 ms |
1112 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
3 ms |
1264 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1048 KB |
Output is correct |
23 |
Correct |
328 ms |
4188 KB |
Output is correct |
24 |
Correct |
313 ms |
4380 KB |
Output is correct |
25 |
Correct |
311 ms |
4188 KB |
Output is correct |
26 |
Correct |
369 ms |
4188 KB |
Output is correct |
27 |
Correct |
146 ms |
4184 KB |
Output is correct |
28 |
Correct |
352 ms |
4184 KB |
Output is correct |
29 |
Correct |
335 ms |
4188 KB |
Output is correct |
30 |
Correct |
323 ms |
4188 KB |
Output is correct |
31 |
Correct |
10 ms |
3932 KB |
Output is correct |
32 |
Correct |
332 ms |
4156 KB |
Output is correct |
33 |
Correct |
330 ms |
4188 KB |
Output is correct |
34 |
Correct |
336 ms |
4184 KB |
Output is correct |
35 |
Correct |
324 ms |
4188 KB |
Output is correct |
36 |
Correct |
327 ms |
4184 KB |
Output is correct |
37 |
Correct |
315 ms |
4184 KB |
Output is correct |
38 |
Correct |
328 ms |
4200 KB |
Output is correct |
39 |
Correct |
328 ms |
4392 KB |
Output is correct |
40 |
Correct |
337 ms |
4392 KB |
Output is correct |
41 |
Correct |
218 ms |
4184 KB |
Output is correct |
42 |
Correct |
240 ms |
4188 KB |
Output is correct |
43 |
Correct |
232 ms |
4188 KB |
Output is correct |
44 |
Correct |
224 ms |
4388 KB |
Output is correct |