Problem:
Problem
無限に広がる二次元格子があります。 Aくんは、変わったマラソンをする予定です。
二次元格子には $N$ 個のコーナーがあり、$1$ から $N$ の番号がついています。
コーナー $i$ の初期位置は 座標 $(X_i, Y_i)$ です。
Aくんはあらかじめ、以下の操作を $K$ 回まで行うことが出来ます。
コーナーを一つ選択する。選んだコーナーの現在の座標を $(p, q)$ とする。
選んだコーナーを座標 $(p, q)$ から座標 $(p+1, q),(p, q+1), (p-1, q), (p, q-1)$ のいずれかへ移動する。
操作を行う回数が、操作に消費するコストです。
操作によって $2$ つ以上のコーナーの座標が同じになっても構いません。
操作の後、Aくんはマラソンを行います。
Aくんははじめコーナー $1$ にいます。コーナー $1, 2, \dots, N$ をこの順に通っていきます。
Aくんは、座標 $(x, y)$ にいるとき、コスト $1$ を消費して、座標 $(x+1, y), (x, y+1), (x-1, y), (x, y-1)$ のいずれかへ動くことができます。
コーナー $N$ がゴールであり、ゴールにたどり着くとマラソンは終わります。
ゴールにたどり着くまでに消費するコストの総和が、マラソンに消費するコストです。
マラソンと操作に消費するコストの合計の最小値を求めてください。
Input
入力は以下の形式で与えられる。
$N$ $K$
$X_1$ $Y_1$
$X_2$ $Y_2$
$\vdots$
$X_N$ $Y_N$
Constraints
$1 \leq N \leq 10^5$
$1 \leq K \leq 10^{12}$
$-10^6 \leq X_i \leq 10^6$
$-10^6 \leq Y_i \leq 10^6$
入力は全て整数である。
Output
マラソンと操作に消費するコストの合計の最小値を一行に出力する。
Sample Input 1
5 15
0 0
7 0
3 -4
5 2
9 -4
Sample Output 1
23
Sample Input 2
14 12
2 3
3 2
5 3
7 0
11 5
13 0
17 5
19 0
23 5
29 0
31 3
37 2
41 3
43 0
Sample Output 2
72

Submitted solutions:

code_snippet: #include <iostream>
#include <set>
#include <climits>
#include <vector>
#include <algorithm>
#include <cassert>
#include <memory>

using namespace std;
using ll = long long;

namespace {
    /* input operator for vectors */
    template<typename T>
    istream& operator>>(istream& is, vector<T>& vs) {
        for (auto&& v : vs) is >> v;
        return is;
    }

    /* output operator for vectors */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os;
        os << vs[0];
        auto it = ++vs.begin();
        for (; it != vs.end(); ++it) {
            os << ' ' << *it;
        }
        return os;
    }
    
    /* output operator for vector of vectors */
    template<typename T>
    ostream& operator<<(ostream& os, const vector<vector<T>>& vs) {
        if (vs.empty()) return os;
        os << vs[0];
        auto it = ++vs.begin();
        for (; it != vs.end(); ++it) {
            os << '\n' << *it;
        }
        return os;
    }
}

namespace {
    ll N, K;
    vector<int> X, Y;
    void input() {
        cin >> N >> K;
        X = vector<int>(N);
        Y = vector<int>(N);
        for (int i = 0; i < N; i++) {
            cin >> X[i] >> Y[i];
        }
    }

    void solve() {
        ll initial_cost = 0;
        for (int i = 0; i < N - 1; i++) {
            initial_cost += abs(X[i+1] - X[i]) + abs(Y[i+1] - Y[i]);
        }
        ll able_to_shortcut = 0;
        auto find_shortcut = [&](vector<int>& X) {
            for (int i = 0; i < N - 2; i++) {
                if (X[i] <= X[i+1] && X[i+1] <= X[i+2]) continue;
                if (X[i] >= X[i+1] && X[i+1] >= X[i+2]) continue;
                ll shortcut = 0;
                if (X[i] > X[i+1] && X[i+1] < X[i+2]) {
                    shortcut = min(X[i] - X[i+1], X[i+2] - X[i+1]);
                    X[i+1] += shortcut;
                } else if (X[i] < X[i+1] && X[i+1] > X[i+2]) {
                    shortcut = min(X[i+1] - X[i], X[i+1] - X[i+2]);
                    X[i+1] -= shortcut;
                }
                able_to_shortcut += shortcut;
            }
        };
        find_shortcut(X);
        find_shortcut(Y);
        cout << initial_cost - min(K, able_to_shortcut) << endl;
    }
}

int main() {
    input();
    solve();
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3904
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

ll N,K;
ll X[SIZE],Y[SIZE];

int main(){

	scanf("%lld %lld",&N,&K);

	ll sum_dist = 0;

	for(ll i = 0; i < N; i++){

		scanf("%lld %lld",&X[i],&Y[i]);
		if(i == 0)continue;

		sum_dist += abs(X[i]-X[i-1])+abs(Y[i]-Y[i-1]);
	}

	ll minus_X = 0,minus_Y = 0;

	for(ll i = 1; i < N-1; i++){

		if(X[i-1] <= X[i+1]){

			if(X[i] > X[i+1]){
				minus_X += X[i]-X[i+1];
				X[i] = X[i+1];
			}else if(X[i] < X[i-1]){

				minus_X += X[i-1]-X[i];
				X[i] = X[i-1];
			}

		}else if(X[i-1] >= X[i+1]){

			if(X[i] < X[i+1]){
				minus_X += X[i+1]-X[i];
				X[i] = X[i+1];
			}else if(X[i] > X[i-1]){
				minus_X += X[i]-X[i-1];
				X[i] = X[i-1];
			}
		}
	}

	for(ll i = 1; i < N-1; i++){

		if(Y[i-1] <= Y[i+1]){

			if(Y[i] > Y[i+1]){
				minus_Y += Y[i]-Y[i+1];
				Y[i] = Y[i+1];
			}else if(Y[i] < Y[i-1]){

				minus_Y += Y[i-1]-Y[i];
				Y[i] = Y[i-1];
			}

		}else if(Y[i-1] >= Y[i+1]){

			if(Y[i] < Y[i+1]){
				minus_Y += Y[i+1]-Y[i];
				Y[i] = Y[i+1];
			}else if(Y[i] > Y[i-1]){
				minus_Y += Y[i]-Y[i-1];
				Y[i] = Y[i-1];
			}
		}
	}

	sum_dist -= min(K,minus_X+minus_Y);

	printf("%lld\n",sum_dist);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4768
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
  int n,k;cin>>n>>k;
  vector<int> x(n),y(n);
  for(int i=0;i<n;i++)cin>>x[i]>>y[i];
  int ans=0;
  for(int i=2;i<n;i++){
    if(x[i-2]<x[i-1]&&x[i]<x[i-1]){
      int tmp=min({x[i-1]-x[i-2],x[i-1]-x[i],k});
      x[i-1]-=tmp;
      k-=tmp;
      ans+=tmp;
    }
    if(x[i-2]>x[i-1]&&x[i]>x[i-1]){
      int tmp=min({x[i-2]-x[i-1],x[i]-x[i-1],k});
      x[i-1]+=tmp;
      k-=tmp;
      ans+=tmp;
    }
    if(y[i-2]<y[i-1]&&y[i]<y[i-1]){
      int tmp=min({y[i-1]-y[i-2],y[i-1]-y[i],k});
      y[i-1]-=tmp;
      k-=tmp;
      ans+=tmp;
    }
    if(y[i-2]>y[i-1]&&y[i]>y[i-1]){
      int tmp=min({y[i-2]-y[i-1],y[i]-y[i-1],k});
      y[i-1]+=tmp;
      k-=tmp;
      ans+=tmp;
    }
  }
  for(int i=1;i<n;i++)ans+=abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4324
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
int64_t MOD=998244353;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long INF = 1LL<<60;


int main() {
  int64_t N,K,c=0,ans=0; cin>>N>>K;
  vector<int64_t> X(N),Y(N);
  rep(i,N){
    cin>>X[i]>>Y[i];
    if(i>0) ans+=abs(X[i]-X[i-1])+abs(Y[i]-Y[i-1]);
  }
  for(int i=1;i<N-1;i++){
    if((X[i]-X[i-1])>0&&(X[i+1]-X[i])<0){
      c+=X[i]-max(X[i-1],X[i+1]);
      X[i]=max(X[i-1],X[i+1]);
    }
    else if((X[i]-X[i-1])<0&&(X[i+1]-X[i])>0){
      c-=X[i]-min(X[i-1],X[i+1]);
      X[i]=min(X[i-1],X[i+1]);
    }
    if((Y[i]-Y[i-1])>0&&(Y[i+1]-Y[i])<0){
      c+=Y[i]-max(Y[i-1],Y[i+1]);
      Y[i]=max(Y[i-1],Y[i+1]);
    }
    else if((Y[i]-Y[i-1])<0&&(Y[i+1]-Y[i])>0){
      c-=Y[i]-min(Y[i-1],Y[i+1]);
      Y[i]=min(Y[i-1],Y[i+1]);
    }
  }
  cout<<ans-min(K,c)<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 4432
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  int64_t K,A,Z;
  cin>>N>>K;
  Z=K;
  vector<int64_t> p(N);
  vector<int64_t> q(N);
  for(int i=0;i<N;i++){
    cin>>p[i]>>q[i];
  }
  for(int i=2;i<N;i++){
    if(p[i-2]<p[i-1]&&p[i]<p[i-1]){
      A=p[i-1]-max(p[i-2],p[i]);
      A=min(A,K);
      K-=A;
      p[i-1]-=A;
    }
    if(p[i-2]>p[i-1]&&p[i]>p[i-1]){
      A=min(p[i-2],p[i])-p[i-1];
      A=min(A,K);
      K-=A;
      p[i-1]+=A;
    }
    if(q[i-2]<q[i-1]&&q[i]<q[i-1]){
      A=q[i-1]-max(q[i-2],q[i]);
      A=min(A,K);
      K-=A;
      q[i-1]-=A;
    }
    if(q[i-2]>q[i-1]&&q[i]>q[i-1]){
      A=min(q[i-2],q[i])-q[i-1];
      A=min(A,K);
      K-=A;
      q[i-1]+=A;
    }
  }
  Z-=K;
  for(int i=1;i<N;i++){
    Z+=abs(p[i-1]-p[i])+abs(q[i-1]-q[i]);
  }
  cout<<Z<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4688
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

ll mylamp(ll a, ll low, ll high){
    if(low > high) swap(low, high);
    return min(max(a, low), high);
}

int main() {
    cin>>N>>K;
    ll len(0), d(0);
    vec x(N), y(N);
    rep(i, N) cin>>x[i]>>y[i];
    reps(i, 1, N) len += abs(x[i-1] - x[i]) + abs(y[i-1] - y[i]);
    reps(i, 1, N - 1){
        ll nx = mylamp(x[i], x[i-1], x[i+1]), ny = mylamp(y[i], y[i-1], y[i+1]);
        d += abs(nx - x[i]) + abs(ny - y[i]);
        x[i] = nx; y[i] = ny;
    }
    cout<<len - min(d, K)<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4432
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define reps(i, n) for (int i = 1; i <= (int) (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end())
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr int DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int sign(int x) { return (x > 0) - (x < 0); }
int gcd(int a, int b) {
    while (b) { swap(a %= b, b); }
    return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int cdiv(int a, int b) { return (a - 1 + b) / b; }
template<typename T> void fin(T mes) {
    cout << mes << endl;
    exit(0);
}
template<typename T> T sq(T x) { return x * x; }
template<typename T, typename U> bool chmax(T &a, const U &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
    os << "(" << rhs.first << ", " << rhs.second << ")";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const deque<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const set<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &rhs) {
    os << "{";
    for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
        os << *itr << (next(itr) != rhs.end() ? ", " : "");
    }
    os << "}";
    return os;
}
struct setup {
    static constexpr int PREC = 20;
    setup() {
        cout << fixed << setprecision(PREC);
        cerr << fixed << setprecision(PREC);
    };
} setup;

signed main() {
    int N, K;
    cin >> N >> K;
    vint X(N), Y(N);
    rep(i, N) { cin >> X[i] >> Y[i]; }
    int ans = 0;
    rep(i, N - 1) { ans += abs(X[i + 1] - X[i]) + abs(Y[i + 1] - Y[i]); }
    int m = 0;
    reps(i, N - 2) {
        if (X[i] < X[i - 1] && X[i] < X[i + 1] || X[i] > X[i - 1] && X[i] > X[i + 1]) {
            if (abs(X[i - 1] - X[i]) < abs(X[i + 1] - X[i])) {
                m += abs(X[i - 1] - X[i]);
                X[i] = X[i - 1];
            } else {
                m += abs(X[i + 1] - X[i]);
                X[i] = X[i + 1];
            }
        }
        if (Y[i] < Y[i - 1] && Y[i] < Y[i + 1] || Y[i] > Y[i - 1] && Y[i] > Y[i + 1]) {
            if (abs(Y[i - 1] - Y[i]) < abs(Y[i + 1] - Y[i])) {
                m += abs(Y[i - 1] - Y[i]);
                Y[i] = Y[i - 1];
            } else {
                m += abs(Y[i + 1] - Y[i]);
                Y[i] = Y[i + 1];
            }
        }
    }
    ans -= min(K, m);
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4688
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 19;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}



void solve() {
	int n;ll k; cin >> n >> k;
	vector<ll> x(n), y(n);
	rep(i, n)cin >> x[i] >> y[i];
	ll ans = 0;
	rep(i, n - 1) {
		ans += abs(x[i + 1] - x[i]);
		ans += abs(y[i + 1] - y[i]);
	}
	rep1(i, n - 2) {
		if (x[i] > x[i - 1] && x[i] > x[i + 1]) {
			ll d = min(k, x[i] - max(x[i - 1], x[i + 1]));
			ans -= d;
			x[i] -= d;
			k -= d;
		}
		if (x[i] < x[i - 1] && x[i] < x[i + 1]) {
			ll d = min(k, min(x[i - 1], x[i + 1]) - x[i]);
			ans -= d;
			x[i] += d;
			k -= d;
		}
		if (y[i] > y[i - 1] && y[i] > y[i + 1]) {
			ll d = min(k, y[i] - max(y[i - 1], y[i + 1]));
			ans -= d; y[i] -= d; k -= d;
		}
		if (y[i] < y[i - 1] && y[i] < y[i + 1]) {
			ll d = min(k, min(y[i - 1], y[i + 1]) - y[i]);
			ans -= d; y[i] += d; k -= d;
		}
	}
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(15);
	init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 12600
final_rank: 17

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

int n;ll k;
ll x[100010],y[100010];

void solve(){
  cin >> n >> k;
  rep(i,n) cin >> x[i] >> y[i];
  ll ans=0;
  rep(i,n-1){
    ans+=abs(x[i+1]-x[i]);
    ans+=abs(y[i+1]-y[i]);
  }
  ll g=0;
  Rep(i,1,n-1){
    if(x[i]<=min(x[i-1],x[i+1])){
      ll v=min(x[i-1],x[i+1]);
      if(k>=abs(v-x[i])){
        k-=abs(v-x[i]);
        g+=abs(v-x[i]);
        x[i]=v;
      }
      else{
        g+=k;
        k=0;
      }
    }
    if(x[i]>=max(x[i-1],x[i+1])){
      ll v=max(x[i-1],x[i+1]);
      if(k>=abs(v-x[i])){
        k-=abs(v-x[i]);
        g+=abs(v-x[i]);
        x[i]=v;
      }
      else{
        g+=k;
        k=0;
      }
    }
    if(y[i]<=min(y[i-1],y[i+1])){
      ll v=min(y[i-1],y[i+1]);
      if(k>=abs(v-y[i])){
        k-=abs(v-y[i]);
        g+=abs(v-y[i]);
        y[i]=v;
      }
      else{
        g+=k;
        k=0;
      }
    }
    if(y[i]>=max(y[i-1],y[i+1])){
      ll v=max(y[i-1],y[i+1]);
      if(k>=abs(v-y[i])){
        k-=abs(v-y[i]);
        g+=abs(v-y[i]);
        y[i]=v;
      }
      else{
        g+=k;
        k=0;
      }
    }
  }
  cout << ans-g << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
accuracy: 1
time_ms: 10
memory_kb: 4924
final_rank: 6

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    int n; ll k;
    cin>>n>>k;
    ll x[2][100010];
    for(int i=0; i<n; i++) cin>>x[0][i]>>x[1][i];
    ll ans=0, s=0;
    for(int t=0; t<2; t++){
        for(int i=0; i<n-1; i++){
            ans+=abs(x[t][i]-x[t][i+1]);
        }
        bool b[100010]={};
        for(int i=1; i<n-1; i++){
            if(x[t][i-1]<x[t][i] && x[t][i]>x[t][i+1]) b[i]=1;
            if(x[t][i-1]>x[t][i] && x[t][i]<x[t][i+1]) b[i]=1;
        }
        for(int i=1; i<n; i++){
            if(b[i-1] && !b[i]){
                vector<ll> v;
                for(int j=i-1; j>=0; j--){
                    v.push_back(abs(x[t][j]-x[t][j+1]));
                    if(!b[j]) break;
                }
                ll pr=min(v[0], v[1]);
                s+=pr;
                for(int j=1; j<(int)v.size()-1; j++){
                    ll a=min(v[j]-pr, v[j+1]);
                    s+=a;
                    pr=a;
                }
            }
        }
    }
    ans-=min(k, s);
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5068
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	ll k; cin >> k;
	vector<ll> x(n),y(n);
	ll res=0;
	for(int i=0;i<n;i++){
		cin >> x[i] >> y[i];
		if(i)res+=abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
	}
	ll c=0;
	for(int i=1;i+1<n;i++){
		if(x[i]==max({x[i],x[i-1],x[i+1]})){
			c+=x[i]-max(x[i-1],x[i+1]);
			x[i]=max(x[i+1],x[i-1]);
		}
		if(x[i]==min({x[i],x[i+1],x[i-1]})){
			c+=min({x[i+1],x[i-1]})-x[i];
			x[i]=min(x[i+1],x[i-1]);
		}
		if(y[i]==max({y[i],y[i-1],y[i+1]})){
			c+=y[i]-max(y[i-1],y[i+1]);
			y[i]=max(y[i+1],y[i-1]);
		}
		if(y[i]==min({y[i],y[i+1],y[i-1]})){
			c+=min({y[i+1],y[i-1]})-y[i];
			y[i]=min(y[i-1],y[i+1]);
		}
	}
	cout << res-min(k,c) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4356
final_rank: 2

code_snippet: #include "bits/stdc++.h"
using namespace std;
//#include "atcoder/all"
//using namespace atcoder;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>istream&operator >>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;}
template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;}
constexpr int INF = 1e18;

signed main(){
	int N, K;
	cin >> N >> K;
	vector<int>x(N), y(N);
	REP(i, N) {
		cin >> x[i] >> y[i];
	}
	auto check = [&](int a, int b, int c) {
		if(a < b && c < b) {
			return -(b - max(a, c));
		}
		if(a > b && c > b) {
			return min(a, c) - b;
		}
		return 0ll;
	};
	int sum = 0, del = 0;
	REP(i, N - 1) {
		sum += abs(x[i] - x[i + 1]) + abs(y[i] - y[i + 1]);
	}
	REP(i, N - 2) {
		int t = check(x[i], x[i + 1], x[i + 2]);
		del += abs(t);
		x[i + 1] += t;
		t = check(y[i], y[i + 1], y[i + 2]);
		del += abs(t);
		y[i + 1] += t;
	}
	out(sum - min(K, del));
}
accuracy: 1
time_ms: 30
memory_kb: 4656
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;

void solve() {
    int n;
    lint k;
    std::cin >> n >> k;
    auto nk = k;

    std::vector<lint> xs(n), ys(n);
    for (int i = 0; i < n; ++i) std::cin >> xs[i] >> ys[i];

    for (int q = 0; q < 2; ++q) {
        for (int i = 1; i + 1 < n; ++i) {
            if (xs[i - 1] < xs[i] && xs[i] > xs[i + 1]) {
                lint d = std::min({xs[i] - xs[i - 1], xs[i] - xs[i + 1], k});
                xs[i] -= d;
                k -= d;

            } else if (xs[i - 1] > xs[i] && xs[i] < xs[i + 1]) {
                lint d = std::min({xs[i - 1] - xs[i], xs[i + 1] - xs[i], k});
                xs[i] += d;
                k -= d;
            }
        }

        std::swap(xs, ys);
    }

    lint ans = nk - k;
    for (int i = 0; i + 1 < n; ++i) {
        ans += std::abs(xs[i + 1] - xs[i]) + std::abs(ys[i + 1] - ys[i]);
    }
    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4652
final_rank: 4

code_snippet: #include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};

ll f(ll x1,ll x2){
    return llabs(x1-x2);
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    ll n,k;
    cin>>n>>k;
    ll sk = k;
    ll x[n],y[n];
    for(int i=0;i<n;i++){
        cin>>x[i]>>y[i];
    }
    
    for(int i=1;i<n-1;i++){
        ll nx = x[i], ny = y[i];
        ll xcost = llabs(x[i]-x[i-1]) + llabs(x[i+1]-x[i]);
        ll ycost = llabs(y[i]-y[i-1]) + llabs(y[i+1]-y[i]);
        // f + min(k,llabs(t-f))*(t-f)/llabs(t-f);
        // x[i] -> x[i+1]
        ll px;
        if(llabs(x[i+1]-x[i])){
            px = x[i] + min(k,f(x[i+1],x[i]))*(x[i+1]-x[i])/llabs(x[i+1]-x[i]);
            if(chmin(xcost,f(px,x[i-1])+f(px,x[i+1]))){
                nx = px;
            }
        }
        if(llabs(x[i]-x[i-1])){
            px = x[i] + min(k,f(x[i],x[i-1]))*(x[i-1]-x[i])/llabs(x[i]-x[i-1]);
            if(chmin(xcost, f(px,x[i-1])+f(px,x[i+1])) || ((xcost == f(px,x[i-1])+f(px,x[i+1])) && llabs(x[i] - nx) > llabs(x[i] - px))){
                nx = px;
            }
        }
        k -= llabs(x[i] - nx);
        
        ll py=0;
        if(f(y[i+1],y[i])){
            py = y[i] + min(k,f(y[i+1],y[i]))*(y[i+1]-y[i])/f(y[i+1],y[i]);
            if(chmin(ycost,f(py,y[i-1])+f(py,y[i+1]))){
                ny = py;
            }
        }
        if(f(y[i],y[i-1])){
            py = y[i] + min(k,f(y[i],y[i-1]))*(y[i-1]-y[i])/f(y[i],y[i-1]);
            if(chmin(ycost, f(py,y[i-1])+f(py,y[i+1]))|| ((ycost == f(py,y[i-1])+f(py,y[i+1])) && llabs(y[i] - ny) > llabs(y[i] - py))){
                ny = py;
            }
        }
        k -= llabs(y[i] - ny);
        x[i] = nx, y[i] = ny;
    }
    ll l = 0, r = n-1;
    // while(k || l<r){
    //     // start -> x[l+1]
    //     ll d = (f(x[l+1],x[l]) + f(y[l+1],y[l]))  * (l+1);
    //     if(k < d){
    //         x[l] += (x[l+1]-x[l])/f(x[l+1],x[l]) * min(k/(l+1),f(x[l+1],x[l]));
    //         k -= min(k/(l+1)*(l+1),f(x[l+1],x[l])*(l+1));
    //         y[l] += (y[l+1]-y[l])/f(y[l+1],y[l]) * min(k/(l+1),f(y[l+1],y[l]));
    //         k -= min(k/(l+1)*(l+1),f(y[l+1],y[l])*(l+1));
    //         break;
    //     }
    //     k -= d;
    //     l++;

    //     if(l==r)break;
    //     // goal -> x[r-1]

    //     d = (f(x[r-1],x[r]) + f(y[r-1],y[r]))  * (n-r);
    //     if(k < d){
    //         x[r] += (x[l-1]-x[r])/f(x[r-1],x[r]) * min(k/(n-r),f(x[r-1],x[r]));
    //         k -= min(k/(n-r)*(n-r),f(x[l+1],x[l])*(n-r));
    //         y[r] += (y[r-1]-y[r])/f(y[r-1],y[r]) * min(k/(n-r),f(y[r-1],y[r]));
    //         k -= min(k/(n-r)*(n-r),f(y[r-1],y[r])*(n-r));
    //         break;
    //     }
    //     k -= d;
    //     r++;
    // }

    ll ans = sk-k;
    for(int i=1;i<n;i++)ans += f(x[i-1],x[i]) + f(y[i-1],y[i]);
    cout << ans << endl;
    
    // cerr << "db" << endl;
    // cout << k << endl;
    // for(int i=0;i<n;i++)cerr << x[i] << " " << y[i] << endl;
    
}
accuracy: 1
time_ms: 10
memory_kb: 4996
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int,int> P;
typedef long long ll;


int main(){
    ll n, k; cin >> n >> k;
    ll ans = k;
    vector<ll> x(n), y(n);
    rep(i,n) cin >> x[i] >> y[i];

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }
    if(n == 2){
        cout << abs( x[1] - x[0] ) + abs( y[1] - y[0] ) << endl;
        return 0;
    }

    for(int i = 1; i <= n-2 ; i++){
        ll fx = x[i+1] - x[i];
        ll lx = x[i] - x[i-1];

        if(fx * lx < 0){
            if(k > 0){
                if(abs(fx) < abs(lx)){
                    x[i] = x[i+1];
                    k-= abs(fx);
                }
                else{
                    x[i] = x[i-1];
                    k-= abs(lx);
                }
            }
        }
    }

    for(int i = 1; i <= n-2 ; i++){
        ll fy = y[i+1] - y[i];
        ll ly = y[i] - y[i-1];

        if(fy * ly < 0){
            if(k > 0){
                if(abs(fy) < abs(ly)){
                    y[i] = y[i+1];
                    k-=abs(fy);
                }
                else{
                    y[i] = y[i-1];
                    k-=abs(ly);
                }
            }
        }
    }

    ans -= k;
    if(k < 0){
        ans -= k;
    }

    rep(i,n-1){
        ans += abs( x[i+1] - x[i] ) + abs( y[i+1] - y[i] );
    }

    cout << ans << endl;
    
}
accuracy: 1
time_ms: 30
memory_kb: 4676
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rrep(i, n) for(ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
#define in(A, n)                                                                                                       \
    rep(i, n) {                                                                                                        \
        cin >> f;                                                                                                      \
        A.push_back(f);                                                                                                \
    }

void print(vl x) {
    for(ll i : x) {
        cout << i << " ";
    }
    cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B,C,D,E,F,G,H;
void input() {
    cin>>n>>q;
rep(i,n){
    cin>>x>>y;
    A.pb(x);
    B.pb(y);
}
C.pb(0);
D.pb(0);
rep(i,n-1){
    C.pb(A[i]-A[i+1]);
    D.pb(B[i]-B[i+1]);
}
C.pb(0);
D.pb(0);
}
void solve() {
	a=0;
	b=0;
    for(ll i=1;i<n;i++){
        if(C[i]*C[i-1]<0||C[i]*C[i+1]<0)E.pb(abs(C[i]));
        else{
        	E.pb(0);
        	a+=abs(C[i]);
        	p+=abs(C[i]);
        }
        if(C[i]*C[i+1]>=0)E.pb(0);
        if(D[i]*D[i-1]<0||D[i]*D[i+1]<0)F.pb(abs(D[i]));
        else{
        	F.pb(0);
        	p+=abs(D[i]);
        	b+=abs(D[i]);
        }
        if(D[i]*D[i+1]>=0)F.pb(0);
    }
   

    p+=accumulate(all(E),0)+accumulate(all(F),0);
    rep(i,E.size()-1){
        k=min(E[i],E[i+1]);
        E[i]-=k;
        E[i+1]-=k;
        a+=k;
    }
    a+=accumulate(all(E),0);

    rep(i,F.size()-1){
        k=min(F[i],F[i+1]);
        F[i]-=k;
        F[i+1]-=k;
        b+=k;
    }

    b+=accumulate(all(F),0);
    print(max(a+b,p-q));
}
int main() {
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    while(codeforces--) {
        solve();
    }
}
accuracy: 0.7368421052631579
time_ms: 10
memory_kb: 7760
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rrep(i, n) for(ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
#define in(A, n)                                                                                                       \
    rep(i, n) {                                                                                                        \
        cin >> f;                                                                                                      \
        A.push_back(f);                                                                                                \
    }

void print(vl x) {
    for(ll i : x) {
        cout << i << " ";
    }
    cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B,C,D,E,F,G,H;
void input() {
    cin>>n>>q;
rep(i,n){
    cin>>x>>y;
    A.pb(x);
    B.pb(y);
}
C.pb(0);
D.pb(0);
rep(i,n-1){
    C.pb(A[i]-A[i+1]);
    D.pb(B[i]-B[i+1]);
}
C.pb(0);
D.pb(0);
}
void solve() {
	a=0;
	b=0;
    for(ll i=1;i<n;i++){
        if(C[i]*C[i-1]<0||C[i]*C[i+1]<0)E.pb(abs(C[i]));
        else{
        	E.pb(0);
        	a+=abs(C[i]);
        	p+=abs(C[i]);
        }
        if(C[i]*C[i+1]>0)E.pb(0);
        if(D[i]*D[i-1]<0||D[i]*D[i+1]<0)F.pb(abs(D[i]));
        else{
        	F.pb(0);
        	p+=abs(D[i]);
        	b+=abs(D[i]);
        }
        if(D[i]*D[i+1]>0)F.pb(0);
    }
   

    p+=accumulate(all(E),0)+accumulate(all(F),0);
    rep(i,E.size()-1){
        k=min(E[i],E[i+1]);
        E[i]-=k;
        E[i+1]-=k;
        a+=k;
    }
    a+=accumulate(all(E),0);

    rep(i,F.size()-1){
        k=min(F[i],F[i+1]);
        F[i]-=k;
        F[i+1]-=k;
        b+=k;
    }

    b+=accumulate(all(F),0);
    print(max(a+b,p-q));
}
int main() {
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    while(codeforces--) {
        solve();
    }
}
accuracy: 0.7368421052631579
time_ms: 10
memory_kb: 7824
final_rank: 20

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

struct point {
    long long x, y;
};

long long n, k; 
vector<point> ps;

pair<long long, long long> calc(long long p1, long long p2, long long p3) {
    if (p1 > p2) swap(p1, p2);
    if (p3 < p1) {
        long long res = p1 - p3;
        p3 = p1;
        return make_pair(res, p1);
    }
    else if (p2 < p3) {
        long long res = p3 - p2;
        p3 = p2;
        return make_pair(res, p2);
    }
    else return make_pair(0, p3);
}

void solve() {
    cin >> n >> k;
    ps.resize(n);
    for (int i = 0; i < n; i++) cin >> ps[i].x >> ps[i].y;

    long long cost = 0;
    for (int i = 1; i < n; i++) {
        cost += abs(ps[i].x - ps[i-1].x) + abs(ps[i].y - ps[i-1].y);
    }

    long long ans = 0;
    for (int i = 0; i < n-2; i++) {
        if (k == 0) break;
        if (k <= calc(ps[i].x, ps[i+2].x, ps[i+1].x).first) {
            cost -= k;
            k = 0;
            ps[i+1].x = calc(ps[i].x, ps[i+2].x, ps[i+1].x).second;
        }
        else {
            cost -= calc(ps[i].x, ps[i+2].x, ps[i+1].x).first;
            k -= calc(ps[i].x, ps[i+2].x, ps[i+1].x).first;
            ps[i+1].x = calc(ps[i].x, ps[i+2].x, ps[i+1].x).second;
        }

        if (k == 0) break;
        if (k <= calc(ps[i].y, ps[i+2].y, ps[i+1].y).first) {
            cost -= k;
            k = 0;
            ps[i+1].y = calc(ps[i].y, ps[i+2].y, ps[i+1].y).second;
        }
        else {
            cost -= calc(ps[i].y, ps[i+2].y, ps[i+1].y).first;
            k -= calc(ps[i].y, ps[i+2].y, ps[i+1].y).first;
            ps[i+1].y = calc(ps[i].y, ps[i+2].y, ps[i+1].y).second;
        }
    }

    cout << cost << '\n';
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4548
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using ll = long long;

using namespace std;

int main() {
  ll N, K;
  cin >> N >> K;

  ll M = K;

  vector<ll> x(N), y(N);
  REP(i, N) cin >> x[i] >> y[i];

  REP(_, 2){
    FOR(i, 1, N-1){
      ll MAX = max(x[i-1], x[i+1]);
      ll MIN = min(x[i-1], x[i+1]);

      ll tmp = 0;
      if(MAX < x[i]){
        tmp = min(x[i]-MAX, K);
        x[i] -= min(x[i]-MAX, K);
      }else if(x[i] < MIN){
        tmp = min(MIN-x[i], K);
        x[i] += min(MIN-x[i], K);
      }
      K -= tmp;

      if(K == 0) break;
    }
    swap(x, y);
  }

  ll ans = M-K;
  REP(i, N-1){
    ans += abs(x[i]-x[i+1]);
    ans += abs(y[i]-y[i+1]);
  }

  cout << ans << "\n";

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4520
final_rank: 13

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <numeric>
#include <iomanip>
#include <stack>
#include <complex>
#include <functional>
#include <tuple>

using namespace std;

#define Rep(i,a,b) for(ll i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;

// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

template<typename T> void chmax(T & a, T b) { a = std::max(a, b); }
template<typename T> void chmin(T & a, T b) { a = std::min(a, b); }

long long solve(int n, vector<int>& v) {
    if (n == 1) return 0LL;

    long long res = 0LL;

    //int sign = (v[1] - v[0] >= 0);
    //bool prevEql = (v[1] == v[0]);

    //for (int i = 2; i < n; ++i) {
    //    if (v[i] == v[i - 1]) {
    //        prevEql = true;
    //        continue;
    //    }

    //    int newSign = (v[i] - v[i - 1] >= 0);

    //    if (!!prevEql && sign != newSign) {
    //        // 合わせる
    //        int newV = v[i - 2];
    //        if (newSign == 0) chmin(newV, v[i]);
    //        else chmax(newV, v[i]);

    //        res += abs(newV - v[i - 1]);
    //        v[i - 1] = newV;
    //    }

    //    sign = newSign;
    //    prevEql = false;
    //}

    for (int i = 1; i < n - 1; ++i) {
        if (v[i] == v[i - 1]) continue;
        if (v[i] == v[i + 1]) continue;

        int lsign = (v[i] - v[i - 1] >= 0);
        int rsign = (v[i + 1] - v[i] >= 0);

        if (lsign + rsign == 1) {
            int newV = v[i - 1];
            if (lsign == 1) chmax(newV, v[i + 1]);
            else chmin(newV, v[i + 1]);

            res += abs(newV - v[i]);
            v[i] = newV;
        }
    }

    //cout << "res : " << res << endl;
    return res;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    long long n, k;
    cin >> n >> k;
    vector<int> x(n);
    vector<int> y(n);
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
    long long left = k;
    int now = 1;
    long long kans = 0;
    for (int i = 1; i < n; i++) {
        kans += abs(x[i - 1] - x[i]) + abs(y[i - 1] - y[i]);
    }

    long long dec = 0LL;
    dec += solve(n, x);
    dec += solve(n, y);

    //cout << "x : ";
    //for (int i = 0; i < n; ++i) {
    //    cout << x[i] << " ";
    //}
    //cout << endl;
    //cout << "y : ";
    //for (int i = 0; i < n; ++i) {
    //    cout << y[i] << " ";
    //}
    //cout << endl;
    
    long long ans = kans - min(k, dec);
    
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3756
final_rank: 1