Problem:
Problem K:
$L_{\infty}$ Jumps
Given two points $(p, q)$ and $(p', q')$ in the XY-plane, the $L_{\infty}$
distance
between them is defined as $max(|p − p'|, |q − q'|)$. In this problem, you are given four integers $n$, $d$, $s$, $t$. Suppose that you are initially standing at point $(0, 0)$ and you need to move to point $(s, t)$. For this purpose, you perform jumps exactly $n$ times. In each jump, you must move exactly $d$ in the $L_{\infty}$ distance measure. In addition, the point you reach by a jump must be a lattice point in the XY-plane. That is, when you are standing at point $(p, q)$, you can move to a new point $(p', q')$ by a single jump if $p'$ and $q'$ are integers and $max(|p − p'|, |q − q'|) = d$ holds.
Note that you cannot stop jumping even if you reach the destination point $(s, t)$ before you perform the jumps $n$ times.
To make the problem more interesting, suppose that some cost occurs for each jump. You are given $2n$ additional integers $x_1$, $y_1$, $x_2$, $y_2$, . . . , $x_n$, $y_n$ such that $max(|x_i|, |y_i|) = d$ holds for each $1 \leq i \leq n$. The cost of the $i$-th (1-indexed) jump is defined as follows: Let $(p, q)$ be a point at which you are standing before the $i$-th jump. Consider a set of lattice points that you can jump to. Note that this set consists of all the lattice points on the edge of a certain square. We assign integer 1 to point $(p + x_i, q + y_i)$. Then, we assign integers $2, 3, . . . , 8d$ to the remaining points in the set in the counter-clockwise order. (Here, assume that the right direction is positive in the $x$-axis and the upper direction is positive in the $y$-axis.) These integers represent the costs when you perform a jump to these points.
For example, Figure K.1 illustrates the points reachable by your $i$-th jump when $d = 2$ and you are at $(3, 1)$. The numbers represent the costs for $x_i = −1$ and $y_i = −2$.
Figure K.1. Reachable points and their costs
Compute and output the minimum required sum of the costs for the objective.
Input
The input consists of a single test case.
$n$ $d$ $s$ $t$
$x_1$ $y_1$
$x_2$ $y_2$
.
.
.
$x_n$ $y_n$
The first line contains four integers. $n$ ($1 \leq n \leq 40$) is the number of jumps to perform. $d$ ($1 \leq d \leq 10^{10}$) is the $L_{\infty}$ distance which you must move by a single jump. $s$ and $t$ ($|s|, |t| \leq nd$) are the $x$ and $y$ coordinates of the destination point. It is guaranteed that there is at least one way to reach the destination point by performing $n$ jumps.
Each of the following $n$ lines contains two integers, $x_i$ and $y_i$ with $max(|x_i|, |y_i|) = d$.
Output
Output the minimum required cost to reach the destination point.
Sample Input 1
3 2 4 0
2 2
-2 -2
-2 2
Sample Output 1
15
Sample Input 2
4 1 2 -2
1 -1
1 0
1 1
-1 0
Sample Output 2
10
Sample Input 3
6 5 0 2
5 2
-5 2
5 0
-3 5
-5 4
5 5
Sample Output 3
24
Sample Input 4
5 91 -218 -351
91 91
91 91
91 91
91 91
91 91
Sample Output 4
1958
Sample Input 5
1 10000000000 -10000000
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for (int i = (a); i < (b); i++)
#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define fo2(i,a,b) for (i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

ll dx[] = {0,1,0,-1};
ll dy[] = {-1,0,1,0};
int n, ut[4], ov[4], v[4];
ll d, s, t, x, y, o[4][40], cum[4][40];
ll xx[40], yy[40];
int main() {
    scanf("%d %lld %lld %lld", &n, &d, &s, &t);
    fo(i,0,n) {
        scanf("%lld %lld", &x, &y);
        xx[i] = x; yy[i] = y;
        if (x == -d) o[0][ut[0]++] = -y;
        else if (y == -d) o[1][ut[1]++] = x;
        else if (x == d) o[2][ut[2]++] = y;
        else o[3][ut[3]++] = -x;
    }
    fo(i,0,4) {
        sort(o[i], o[i]+ut[i]);
        fo(j,0,ut[i]) {
            cum[i][j] = o[i][j];
            if (j) cum[i][j] += cum[i][j-1];
        }
    }

    ll ans = 1e18;
    fo2(ov[0],0,n+1) fo2(ov[1],0,n+1) fo2(ov[2],0,n+1) fo2(ov[3],0,n+1) {
        fo(i,0,4) v[i] = ov[i];

        int cnt[4];
        fo(i,0,4) cnt[i] = ut[i];
        fo(i,0,8) {
            int ii = i%4;
            int mv = min(v[ii], cnt[ii]);
            v[ii] -= mv;
            cnt[ii] -= mv;
            cnt[(ii+1)%4] += mv;
        }
        int g = 1;
        fo(i,0,4) if (v[i]) g = 0;
        if (!g) continue;

        ll tans = n;
        ll ts = s, tt = t;
        fo(i,0,n) {
            ts -= xx[i];
            tt -= yy[i];
        }

      /*  fo(i,0,8) {
            int ii = i%4;
            while (v[ii] && !ss[ii].empty()) {
                v[ii]--;
                ll num = *ss[ii].rbegin();
                ss[ii].erase(ss[ii].find(num));
                tans += d - num;
                ts -= dx[ii] * (d - num);
                tt -= dy[ii] * (d - num);
                ss[(ii+1)%4].insert(-d);
            }
        }
        int g = 1;
        fo(j,0,4) if (v[j]) g = 0;
        if (!g) continue;
       */
//        printf("%d %d %d %d\n", ov[0], ov[1], ov[2], ov[3]);

        ll sum[4] = {0,0,0,0};
        fo(i,0,4) {
            int ovout = ov[i], ovin = ov[(i+3)%4];
            int bal = ut[i] + ovin - ovout;
            int keep = max(0, ut[i] - ovout);
            if (keep) {
                sum[i] += d*keep - cum[i][keep-1];
            }
            sum[i] += 2*d* min(bal, ovin);

            int origout = min(ovout, ut[i]);
            ll mv = 0;
            if (origout) {
                mv += d*origout - (cum[i][ut[i] - 1] - (origout==ut[i] ? 0 : cum[i][ut[i] - 1 - origout]));
            }
            mv += 2*d * max(0, ovout - ut[i]);
            tans += mv;

            ts -= dx[i] * mv;
            tt -= dy[i] * mv;

            assert(keep + origout == ut[i]);
        }

  //      fo(i,0,4) printf("%lld\n", sum[i]);

        if (tt < 0 && abs(tt) > sum[0]) continue;
        if (tt > 0 && tt > sum[2]) continue;
        if (ts < 0 && abs(ts) > sum[3]) continue;
        if (ts > 0 && ts > sum[1]) continue;

        tans += abs(ts) + abs(tt);

        ans = min(ans, tans);
    }
    printf("%lld\n", ans);

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3572
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> bool chmax(T& x, U y){
	if(x<y){ x=y; return true; }
	return false;
}
template<class T,class U> bool chmin(T& x, U y){
	if(y<x){ x=y; return true; }
	return false;
}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class T>
V<T> Vec(size_t a) {
    return V<T>(a);
}
template<class T, class... Ts>
auto Vec(size_t a, Ts... ts) {
  return V<decltype(Vec<T>(ts...))>(a, Vec<T>(ts...));
}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<class D> D divFloor(D a, D b){
	return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0);
}
template<class D> D divCeil(D a, D b) {
	return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0);
}
	struct Waf{
		ll x,y;
		ll cost,d;
		friend ostream& operator<<(ostream &o,const Waf& x){ return o<<"("<<x.x<<","<<x.y<<"), " << x.cost << " " << x.d;}
	};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N; ll D,X,Y; cin >> N >> D >> X >> Y;
	V<ll> f;
	rep(i,N){
		ll x,y; cin >> x >> y;
		auto calcf = [&](ll x,ll y){
			if(y == D) return D-x;
			if(x == -D) return 3*D-y;
			if(y == -D) return 5*D+x;
			return 7*D+y;
		};
		f.pb(calcf(x,y));
	}
	sort(all(f));
	auto waf = Vec<Waf>(4,N,N+1);
	rep(t,4){
		rep(l,N){
			rep1(n,N){
				auto& w = waf[t][l][n];
				rep(k,n){
					int i = (l+k)%N;
					if(f[i] <= 2*D){
						w.x += D-f[i];
						w.y += D;
						w.cost += 0;
						w.d += 2*D-f[i];
					}else{
						w.x += D;
						w.y += D;
						w.cost += 8*D-f[i];;
						w.d += 2*D;
					}
				}
				rep(_,t){
					w.y = -w.y; swap(w.x,w.y);
				}
			}
		}
		rep(i,N) f[i] = (f[i]+6*D)%(8*D);
	}
	ll ans = TEN(18);
	rep(s0,N){
		rep(n0,N+1) rep(n1,N+1-n0) rep(n2,N+1-n0-n1){
			int n3 = N-n0-n1-n2;
			int s1 = (s0+n0)%N, s2 = (s1+n1)%N, s3 = (s2+n2)%N;
			V<Waf> w = {waf[0][s0][n0], waf[1][s1][n1], waf[2][s2][n2], waf[3][s3][n3]};
			// x--, y--, x++, y++
			ll x=0,y=0,cost=0;
			rep(i,4) x += w[i].x, y += w[i].y, cost += w[i].cost;
			ll dx = X-x, dy = Y-y;
			bool ok = true;
			cost += abs(dx) + abs(dy);
			if(dx >= 0){
				if(dx > w[2].d) ok = false;
			}else{
				if(-dx > w[0].d) ok = false;
			}
			if(dy >= 0){
				if(dy > w[3].d) ok = false;
			}else{
				if(-dy > w[1].d) ok = false;
			}
			cost += N;
			if(ok) chmin(ans,cost);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3724
final_rank: 1

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[ui+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
			//		printf("%lld ",cost);
			long long D1=cost;
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
					
			//		printf("%lld ",cost);
			long long D2=cost;
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
			//		printf("%lld ",cost);
			long long D3=cost;
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[li+j];
							else za=d;
							can+=za+d;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[ri+j];
							else za=-d;
							can+=d-za;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
				//	if(-3000<xsum-dx&&xsum-dx<3000)printf("%d %d %d %d %d: %lld %lld %lld %lld %lld %lld %lld %lld\n",D,R,U,L,i,xsum,dx,ysum,dy, D1,D2,D3,cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 1
time_ms: 160
memory_kb: 1036
final_rank: 2

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[ui+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
			//		printf("%lld ",cost);
			long long D1=cost;
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
					
			//		printf("%lld ",cost);
			long long D2=cost;
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
			//		printf("%lld ",cost);
			long long D3=cost;
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							can+=za+d;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							can+=d-za;
						}//if(can<rem)continue;cost+=rem;
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
			//		if(-40000<xsum-dx&&xsum-dx<40000)printf("%d %d %d %d %d: %lld %lld %lld %lld %lld %lld %lld %lld\n",D,R,U,L,i,xsum,dx,ysum,dy, D1,D2,D3,cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.14285714285714285
time_ms: 80
memory_kb: 1028
final_rank: 5

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[ui+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
			//		printf("%lld ",cost);
			long long D1=cost;
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}
						if(can<rem)continue;
						cost+=rem;
	/*					if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}
					
			//		printf("%lld ",cost);
			long long D2=cost;
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
			//		printf("%lld ",cost);
			long long D3=cost;
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							can+=za+d;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							can+=d-za;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}
			//		if(-40000<xsum-dx&&xsum-dx<40000)printf("%d %d %d %d %d: %lld %lld %lld %lld %lld %lld %lld %lld\n",D,R,U,L,i,xsum,dx,ysum,dy, D1,D2,D3,cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.14285714285714285
time_ms: 60
memory_kb: 1028
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[di+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
			//		printf("%lld ",cost);
			int D1=cost;
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}
						if(can<rem)continue;
						cost+=rem;
	/*					if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}
					
			//		printf("%lld ",cost);
			int D2=cost;
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
			//		printf("%lld ",cost);
			int D3=cost;
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							can+=za+d;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							can+=d-za;
						}if(can<rem)continue;cost+=rem;
				/*		if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;*/
					}
				//	if(cost<4700)printf("%d %d %d %d %d: %lld %lld %lld %lld\n",D,R,U,L,i,D1,D2,D3,cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.009523809523809525
time_ms: 50
memory_kb: 1024
final_rank: 6

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
				//	printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[di+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
		//			printf("%lld ",cost);
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
					
			//		printf("%lld ",cost);
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
		//			printf("%lld ",cost);
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							can+=za+d;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							can+=d-za;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
				//	printf("%lld\n",cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.009523809523809525
time_ms: 70
memory_kb: 1028
final_rank: 10

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[di+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
		//			printf("%lld ",cost);
					if(xsum>dx){
						long long rem=xsum-dx;
						long long can=0;
						long long rev=0;
						long long val=999999999999999999LL;
						int ru=0;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							can+=za+d;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										rev+=r[j];
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(xsum<dx){
						long long rem=dx-xsum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							can+=d-za;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										rev+=r[j]-4*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}
					
			//		printf("%lld ",cost);
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
		//			printf("%lld ",cost);
					if(ysum>dy){
						long long rem=ysum-dy;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							can+=za+d;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										rev+=r[j]-2*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
						cost+=val;
					}else if(ysum<dy){
						long long rem=dy-ysum;
						long long can=0;int ru=0;
						long long rev=0;
						long long val=999999999999999999LL;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							can+=d-za;
						}
						if(rem){
							if(rem<=can)val=min(val,rem);
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										rev+=r[j]-6*d;
										ru++;
										if(rem<=can+rev){
											val=min(val,8*d*ru-min(rev,rem)+max(0LL,rem-rev));
										}
									}
								}
							}
						}
					}
			//		printf("%lld\n",cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.009523809523809525
time_ms: 60
memory_kb: 1028
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
					
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[di+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
		//			printf("%lld ",cost);
					if(xsum>dx){
						long long rem=xsum-dx;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							if(rem<=za+d){
								cost+=rem;rem=0;break;
							}
							cost+=za+d;rem-=za+d;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										if(rem<=r[j]){
											cost+=8*d-rem;rem=0;break;
										}
										cost+=8*d-r[j];rem-=r[j];
									}
								}
							}
						}
					}else if(xsum<dx){
						long long rem=dx-xsum;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							if(rem<=d-za){
								cost+=rem;rem=0;break;
							}
							cost+=d-za;rem-=d-za;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										if(rem<=r[j]-4*d){
											cost+=8*d-rem+4*d;rem=0;break;
										}
										cost+=8*d-r[j]+4*d;rem-=r[j]-4*d;
									}
								}
							}
						}
					}
					
			//		printf("%lld ",cost);
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
		//			printf("%lld ",cost);
					if(ysum>dy){
						long long rem=ysum-dy;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							if(rem<=za+d){
								cost+=rem;rem=0;break;
							}
							cost+=za+d;rem-=za+d;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										if(rem<=r[j]-2*d){
											cost+=8*d-(rem-2*d);rem=0;break;
										}
										cost+=8*d-r[j]+2*d;rem-=r[j]-2*d;
									}
								}
							}
						}
					}else if(ysum<dy){
						long long rem=dy-ysum;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							if(rem<=d-za){
								cost+=rem;rem=0;break;
							}
							cost+=d-za;rem-=d-za;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										if(rem<=r[j]-6*d){
											cost+=8*d-rem+6*d;rem=0;break;
										}
										cost+=8*d-r[j]+6*d;rem-=r[j]-6*d;
									}
								}
							}
						}
					}
			//		printf("%lld\n",cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.009523809523809525
time_ms: 60
memory_kb: 1028
final_rank: 7

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
long long x[100];
long long y[100];
long long r[100];
int main(){
	int n;
	scanf("%d",&n);
	long long d,s,t;
	scanf("%lld%lld%lld",&d,&s,&t);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",x+i,y+i);
		if(y[i]==-d)r[i]=x[i]+d;
		else if(x[i]==d)r[i]=2*d+y[i]+d;
		else if(y[i]==d)r[i]=4*d-x[i]+d;
		else r[i]=6*d-y[i]+d;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n-1;j++){
			if(r[j]>r[j+1]){
				swap(x[j],x[j+1]);
				swap(y[j],y[j+1]);
				swap(r[j],r[j+1]);
			}
		}
	}
	for(int i=0;i<n;i++){
		x[i+n]=x[i];y[i+n]=y[i];r[i+n]=r[i];
	}
	long long ret=999999999999999999LL;
	for(int L=0;L<=n;L++){
		for(int R=0;L+R<=n;R++){
			for(int U=0;L+R+U<=n;U++){
				int D=n-L-R-U;
				long long dx=s-d*(R-L);
				long long dy=t-d*(U-D);
				if(dx<-d*(U+D)||dx>d*(U+D))continue;
				if(dy<-d*(R+L)||dy>d*(R+L))continue;
				for(int i=0;i<n;i++){
					
			//		printf("%d %d %d %d %d: ",D,R,U,L,i);
					int di=i;
					int ri=di+D;
					int ui=ri+R;
					int li=ui+U;
					long long xsum=0;
					long long ysum=0;
					long long cost=0;
					for(int j=0;j<D;j++){
						if(r[di+j]/(2*d)==0)xsum+=x[di+j];
						else{
							xsum-=d;
							cost+=8*d-r[di+j];
						}
					}
					for(int j=0;j<U;j++){
						if(r[ui+j]/(2*d)==2)xsum+=x[ui+j];
						else{
							xsum+=d;
							cost+=8*d-(r[di+j]+4*d)%(8*d);
						}
					}
			//		printf("%lld %lld\n",xsum,dx);
		//			printf("%lld ",cost);
					if(xsum>dx){
						long long rem=xsum-dx;
						for(int j=0;j<U;j++){
							long long za;
							if(4*d<=r[ui+j]&&r[ui+j]<6*d)za=x[ui+j];
							else za=d;
							if(rem<=za+d){
								cost+=rem;rem=0;break;
							}
							cost+=za+d;rem-=za+d;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((di<=j&&j<di+D)||(di<=j+n&&j+n<di+D)){
									if(0<r[j]&&r[j]<2*d){
										if(rem<=r[j]){
											cost+=8*d-rem;rem=0;break;
										}
										cost+=8*d-rem;rem-=r[j];
									}
								}
							}
						}
					}else if(xsum<dx){
						long long rem=dx-xsum;
						for(int j=0;j<D;j++){
							long long za;
							if(0<=r[di+j]&&r[di+j]<2*d)za=x[di+j];
							else za=-d;
							if(rem<=d-za){
								cost+=rem;rem=0;break;
							}
							cost+=d-za;rem-=d-za;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((ui<=j&&j<ui+U)||(ui<=j+n&&j+n<ui+U)){
									if(4*d<r[j]&&r[j]<6*d){
										if(rem<=r[j]-4*d){
											cost+=8*d-rem+4*d;rem=0;break;
										}
										cost+=8*d-rem+4*d;rem-=r[j]-4*d;
									}
								}
							}
						}
					}
					
			//		printf("%lld ",cost);
					for(int j=0;j<R;j++){
						if(r[ri+j]/(2*d)==1)ysum+=y[ri+j];
						else{
							ysum-=d;
							cost+=8*d-(r[ri+j]+6*d)%(8*d);
						}
					}
					for(int j=0;j<L;j++){
						if(r[li+j]/(2*d)==3)ysum+=y[li+j];
						else{
							ysum+=d;
							cost+=8*d-(r[li+j]+2*d)%(8*d);
						}
					}
			//		printf("%lld ",cost);
					if(ysum>dy){
						long long rem=ysum-dy;
						for(int j=0;j<L;j++){
							long long za;
							if(6*d<=r[li+j]&&r[li+j]<8*d)za=y[di+j];
							else za=d;
							if(rem<=za+d){
								cost+=rem;rem=0;break;
							}
							cost+=za+d;rem-=za+d;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((ri<=j&&j<ri+R)||(ri<=j+n&&j+n<ri+R)){
									if(2*d<r[j]&&r[j]<4*d){
										if(rem<=r[j]-2*d){
											cost+=8*d-(rem-2*d);rem=0;break;
										}
										cost+=8*d-rem+2*d;rem-=r[j]-2*d;
									}
								}
							}
						}
					}else if(ysum<dy){
						long long rem=dy-ysum;
						for(int j=0;j<R;j++){
							long long za;
							if(2*d<=r[ri+j]&&r[ri+j]<4*d)za=y[di+j];
							else za=-d;
							if(rem<=d-za){
								cost+=rem;rem=0;break;
							}
							cost+=d-za;rem-=d-za;
						}
						if(rem){
							for(int j=n-1;j>=0;j--){
								if((li<=j&&j<li+L)||(li<=j+n&&j+n<li+L)){
									if(6*d<r[j]&&r[j]<8*d){
										if(rem<=r[j]-6*d){
											cost+=8*d-rem+6*d;rem=0;break;
										}
										cost+=8*d-rem+6*d;rem-=r[j]-6*d;
									}
								}
							}
						}
					}
			//		printf("%lld\n",cost);
					ret=min(ret,cost);
				}
			}
		}
	}
	printf("%lld\n",ret+n);
}
accuracy: 0.009523809523809525
time_ms: 60
memory_kb: 1028
final_rank: 7