#include using namespace std; typedef long long ll; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b #define vl vector #define vii vector> #define vll vector> #define vvi vector> #define vvl vector> #define vvii vector>> #define vvll vector>> #define vst vector #define pii pair #define pll pair #define pb push_back #define all(x) (x).begin(),(x).end() #define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end()) #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=998244353,MAX=300005,INF=15<<26; int dp[2005][205][2]; int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int N,M,C,X;cin>>N>>M>>C>>X; vvi A(N,vi(M)); for(int i=0;i>A[i][j]; } } int l=-INF,r=INF; while(r-l>1){ int m=(l+r)/2; for(int i=0;i<=N;i++){ for(int j=0;j=m){ dp[1][j][0]=0; } if(A[0][j]-C>=m&&A[1][j]-C>=m){ dp[2][j][1]=2; } } for(int i=1;i=m) chmax(dp[i+1][j][0],ma); chmax(ma,dp[i][j][0]); chmax(ma,dp[i][j][1]); } ma=-INF; for(int j=M-1;j>=0;j--){ if(A[i][j]>=m) chmax(dp[i+1][j][0],ma); chmax(ma,dp[i][j][0]); chmax(ma,dp[i][j][1]); } for(int j=0;j=0&&A[i][j]-C>=m) chmax(dp[i+1][j][1],dp[i][j][1]+1); } if(i+1=m&&A[i+1][j]-C>=m) chmax(dp[i+2][j][1],ma+2); chmax(ma,dp[i][j][0]); } ma=-INF; for(int j=M-1;j>=0;j--){ if(A[i][j]-C>=m&&A[i+1][j]-C>=m) chmax(dp[i+2][j][1],ma+2); chmax(ma,dp[i][j][0]); } } } bool ok=false; for(int j=0;j=X) ok=true; if(dp[N][j][1]>=X) ok=true; } if(ok) l=m; else r=m; } cout<