Problem:
Problem E: Light
Problem
$W \times H$ の二次元のマス上に$N$個の街灯がある。
がっちょ君は$(1,1)$からスタートして$(W,H)$に行きたい。
がっちょ君は暗いところが怖いので、街灯により明るくなっているマスしか歩きたくない。
最初、すべての街灯はその街灯のあるマスのみを明るくしている。
そこで、がっちょ君は好きな街灯$i$に対してコスト$r_i$を設定することにした。なお、コストを設定しない街灯があってもよい。
街灯$i$はコスト$r_i$を消費することにより、その街灯を中心にマンハッタン距離で$r_i$以内の範囲を明るくすることができる。ただしコストは正の整数とする。
がっちょ君は上下左右のいずれかの方向に隣接するマスに移動する事ができる。
がっちょ君は$r_i$の合計値を最小になるように設定することにした。そのときの合計値を求めよ。
2つの地点 $(a,b)$ と $(c,d)$ 間のマンハッタン距離は $|a−c|$+$|b−d|$ で表される。
Input
入力は以下の形式で与えられる。
$W$ $H$ $N$
$x_1$ $y_1$
...
$x_N$ $y_N$
入力はすべて整数で与えられる。
1行目に$W$と$H$と$N$が空白区切りで与えられる。
続く$N$行に街灯$i$の座標$($$x_i$,$y_i$$)$が空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
$1 \leq W \leq 500$
$1 \leq H \leq 500$
$1 \leq N \leq 100$
$1 \leq N \leq W \times H$
$1 \leq $$x_i$$ \leq W$
$1 \leq $$y_i$$ \leq H$
同じ座標に複数の街灯はない
Output
$r_i$の合計値の最小値を1行に出力せよ。
Sample Input 1
10 10 1
6 6
Sample Output 1
10
Sample Input 2
5 10 3
3 9
2 8
5 1
Sample Output 2
8
Sample Input 3
1 1 1
1 1
Sample Output 3
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstdio>
#include <vector>
#include <iostream>
#include <utility>
#include <queue>
#include <map>
#include <complex>
#define fir first
#define sec second
#define sz(s) (s).size()
#define pb push_back
#define get(n) scanf("%d",&n);
#define gets(s) string s;cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i,j) for(int (i)=0;(i)<(j);(i)++)
#define For(i,j,k) for(int (i)=(j);(i)<(k);(i)++)
#define drep(i,j) for(int (i)=(j);(i)>=0;(i)--)
#define Ford(i,j,k) for(int (i)=(j);i>=(k);i--)
#define fore(c,v) for(auto (c): (v))
#define lp for(int __=0;__<n;i++)
#define mem(a,b) memset(a,b,sizeof(a));
#define dump(x)  std::cout << #x << " = " << (x) << std::endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
using ull = unsigned long long int;
using ll = long long;
using ld = long double;
using pii = std::pair<int,int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int> ;
using vvi = std::vector<vi> ;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double> ;
using vvd = std::vector<vd> ;
using qi = std::queue<int> ;
using vpii = std::vector<std::pair<int, int> >;
using vpll = std::vector<pll>;
using namespace std;

const int Mod = (1e9) + 7;
const int INF = 1e9 + 19;
const ll INFL = 1e18 + 19;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
const double EPS = 1e-10;
//_____________________________________Templates_________________________________________//

template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;}
template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;}
template<class T> inline void pri(T a){cout << a << endl;}
template<class Z> using vec = vector<Z>;
template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
//mainly use for dynamic prog
template<class T1, class T2>
void update(T1 &a, T2 b){
  a += b;
  if(a > Mod) a %= Mod;
}

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

bool pairsort(pll pl, pll pr){
  if(pl.first == pr.first)return pl.second > pr.second;
  return pl.first < pr.first;
}

int cntbit(ll a,int n,int j){int res = 0;For(i,j,n){if(a>>i & 1){res++;}}return res;}
vector<int> make_bit(int a){vector<int> res; for(int i=31;i>=0;i--)if(a&(1<<i))res.pb(i);return res;}
bool stdbit(int a, int b){return a&(1 << b); }
int GCD(int a, int b){if(b > a)return GCD(b,a);if(a%b == 0)return b;else return GCD(b, a%b);}
int LCM(int a, int b){return a*b/GCD(a,b);}
int roundup(int a, int b){if(a % b == 0)return a/b;else return (a+b)/b;}
int rounddown(int a, int b){if(a%b == 0)return a/b;else {return (a-b)/b;}}
ll pow(ll a, ll n){ll res = 1;while(n > 0){if(n&1)res *= a; a *= a; n = n >> 1;}return res;}
ll GetDiviserCount(ll N)//約数の個数
{
  ll res = 1;
  For(i,2,sqrt(N)+1)
  {
    ll cnt = 0;
    while(N%i == 0)
    {
      cnt++;
      N /= i;
    }
    res *= (cnt + 1);
    if(N == 1)break;
  }
  if(N != 1)res *= 2;
  return res;
}
vll GetDivisor(ll N)//約数列挙
{
  vll res;
  for(ll i = 1;i*i <= N;i++)
  {
    if(N%i == 0)
    {
      res.pb(i);
      if(i*i != N)res.pb(N/i);
    }
  }
  sort(All(res));
  return res;
}


struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%Mod+Mod)%Mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= Mod) x -= Mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += Mod-a.x) >= Mod) x -= Mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= Mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(Mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
  friend ostream& operator<<(ostream& os, const mint& a);
};
ostream& operator<<(ostream& os, const mint& a)
{
  os << a.x;
  return os;
}
struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < Mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};
 
template<class Z>
struct Matrix
{
  using mat = Matrix<Z>;
  vector<vector<Z>> m_dat;
  int m_h;
  int m_w;
  Matrix(int h, int w) : m_h(h), m_w(w) ,m_dat(h,vector<Z>(w)) {}
  vector<Z> &operator[] (int idx)
  {
    return m_dat[idx];
  }
  mat Multiple(mat &a)
  {
    mat C(m_h, a.m_w);
    for(int i=0;i<m_h;i++)
    {
      for(int j=0;j<a.m_w;j++)
      {
        for(int k=0;k<m_w;k++)
        {
         C[i][j] += m_dat[i][k] * a[k][j];
        }
      }
    }
    return C;
  }
  mat Pow(ll x)
  {
    mat B(m_h,m_w);
    for(int i=0;i<m_h;i++)
    {
      B[i][i] = 1;
    }
    mat A = *this;
    while(x > 0)
    {
      if(x&1)B = B.Multiple(A);
      A = A.Multiple(A);
      x = x >> 1;
    }
    return B;
  }
  //friend ostream& operator<<(ostream &os, const mat &A);
};
/*
template<class Z>
ostream& operator<<(ostream &os, Matrix<Z>& A)
{
  for(int i=0;i<A.m_h;i++)
  {
    for(int j=0;j<A.m_w;j++)
    {
      os << A[i][j] << " ";
    }
    os << endl;
  }
  return os;
}
template<class T>
using mat = Matrix<T>;
*/
//_____________________　following sorce code_________________________//
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;

int n,m,k;
ll N;
int h,w;
string S;
int a,b,c;
vi v;
int ans;
double x,y,z;
vector<int> G[1010101];
struct Dat
{
  int Num,cost,Man;
  Dat() {}
  Dat(int pos, int cost,int Man) : Num(pos), cost(cost),Man(Man){}
  bool operator>(const Dat &a) const
  {
    return cost > a.cost;
  }
};
signed main (int argc, char* argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  IN(w,h);
  w--;h--;
  IN(n);
  vi x(n), y(n);
  rep(i,n)
  {
    IN(x[i],y[i]);
    x[i]--;
    y[i]--;
  }
  ans = INF;
  min_priority_queue<Dat> qu;
  vector<vector<int>> dist(n,vi(1010,INF));
  rep(i,n)
  {
    int k = x[i] + y[i];
    dist[i][k] = k;
    qu.push(Dat(i,k,k));
  }
  while(!qu.empty())
  {
    auto e = qu.top();qu.pop();
    int curr = e.Num;
    int cost = e.cost;
    int Man = e.Man;
    if(dist[curr][Man] < cost)continue;
    //dist[curr][Man] = cost;
    rep(i,n)
    {
      if(curr == i)continue;
      int d = max(0,abs(x[i]-x[curr]) + abs(y[i]-y[curr]) - 1 - Man);
      if(dist[i][d] > d + cost)
      {
        dist[i][d] = d+cost;
        qu.push(Dat(i,d+cost,d));
      }
    }
  }
  int ans = INF;
  rep(i,n)
  {
    rep(j,1010)
    {
      chmin(ans, dist[i][j] + max(0,abs(w-x[i]) + abs(h-y[i]) - j));
    }
  }
  cout << ans << endl;
  //for(auto c : ans){cout << c << endl;}
  //cout << fixed << setprecision(15) << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 27972
final_rank: 13

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>
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 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;
typedef complex<ld> Point;
const ld eps = 1e-2;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

int w, h, n;
int x[100], y[100];
int d[100][1001];
struct vertice {
	int id, bl; int cost;
	bool operator<(const vertice &v)const {
		if (v.cost != cost)return cost > v.cost;
		if (v.id != id)return v.id > id;
		else return v.bl > bl;
	}
};
priority_queue<vertice> q;
void init() {
	rep(i, 100) {
		rep(j, 1001) {
			d[i][j] = mod;
		}
	}
}
int main() {
	init();
	cin >> w >> h >> n;
	rep(i, n) {
		cin >> x[i] >> y[i];
	}
	rep(i, n) {
		int z = x[i] - 1 + y[i] - 1;
		q.push({ i,z,z });
		d[i][z] = z;
	}
	int ans = mod;
	//dijkstra
	while (!q.empty()) {
		vertice v = q.top(); q.pop();
		int id = v.id,bl=v.bl,cost=v.cost;
		int dis = h - x[id] + w - y[id];
		ans = min(ans, cost+max(0, dis - bl));
		rep(i, n) {
			if (i == id)continue;
			int dis = abs(x[id] - x[i]) + abs(y[id] - y[i]);
			dis = max(0, dis - bl-1);
			if (d[i][dis] > cost + dis) {
				d[i][dis] = cost + dis;
				q.push({ i,dis,cost + dis });
			}
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4284
final_rank: 2

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>
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 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;
typedef complex<ld> Point;
const ld eps = 1e-2;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

int w, h, n;
int x[100], y[100];
int d[100][1001];
struct vertice {
	int id, bl; int cost;
	bool operator<(const vertice &v)const {
		if (v.cost != cost)return cost > v.cost;
		if (v.id != id)return v.id > id;
		else return v.bl > bl;
	}
};
priority_queue<vertice> q;
void init() {
	rep(i, 100) {
		rep(j, 1001) {
			d[i][j] = mod;
		}
	}
}
int main() {
	init();
	cin >> w >> h >> n;
	rep(i, n) {
		cin >> x[i] >> y[i];
	}
	rep(i, n) {
		int z = x[i] - 1 + y[i] - 1;
		q.push({ i,z,z });
		d[i][z] = z;
	}
	int ans = mod;
	//dijkstra
	while (!q.empty()) {
		vertice v = q.top(); q.pop();
		int id = v.id,bl=v.bl,cost=v.cost;
		int dis = h - x[id] + w - y[id];
		ans = min(ans, cost+max(0, dis - bl));
		rep(i, n) {
			if (i == id)continue;
			int dis = abs(x[id] - x[i]) + abs(y[id] - y[i]);
			dis = max(0, dis - bl-1);
			if (d[i][dis] > cost + dis) {
				d[i][dis] = cost + dis;
				q.push({ i,dis,cost + dis });
			}
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4224
final_rank: 4

code_snippet: //外灯をつける→次の外灯まで移動する→外灯が消える、N * (W + H)頂点の拡張ダイクストラ。スタートとゴールは大きさ-1の外灯。
//O(N^2 * (W + H) * logほげ)くらい。
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
#include <tuple>
#include <cmath>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

int INF = 1e+9;
int w, h, n;
int x[102], y[102];
int minCost[102][1100];	//minCost[場所][外灯の大きさ + 1]

signed main() {
	int i, j;
	
	cin >> w >> h >> n;
	rep(i, n) { cin >> x[i] >> y[i]; }
	x[n] = 1;
	y[n] = 1;
	x[n + 1] = w;
	y[n + 1] = h;
	n += 2;
	
	rep(i, n) rep(j, 1100) minCost[i][j] = INF;
	
	typedef tuple<int, int, int> T;
	priority_queue<T, vector<T>, greater<T> > que;
	
	minCost[n - 2][0] = 0;
	que.push(T(0, n - 2, 0));
	while (!que.empty()) {
		T now = que.top(); que.pop();
		int cst = get<0>(now);
		int pos = get<1>(now);
		int sz = get<2>(now) - 1;
		
		rep(j, n) {
			if (pos == j) continue;
			int lowSz = abs(x[j] - x[pos]) + abs(y[j] - y[pos]) - sz - 1;
			if (j == n - 2 && lowSz >= 0) { continue; }
			if (j == n - 1 && lowSz >= 0) { lowSz++; }
			if (lowSz < 0) lowSz = 0;
			if (minCost[j][lowSz + 1] > cst + lowSz) {
				minCost[j][lowSz + 1] = cst + lowSz;
				que.push(T(cst + lowSz, j, lowSz + 1));
			}
		}
	}
	
	int ans = INF;
	rep(i, 1100) ans = min(ans, minCost[n - 1][i]);
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5380
final_rank: 5

code_snippet: //外灯をつける→次の外灯まで移動する→外灯が消える、N * (W + H)頂点の拡張ダイクストラ。スタートとゴールは大きさ-1の外灯。
//O(N^2 * (W + H) * logほげ)くらい。
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
#include <tuple>
#include <cmath>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

int INF = 1e+9;
int w, h, n;
int x[102], y[102];
int minCost[102][1100];	//minCost[場所][外灯の大きさ + 1]

signed main() {
	int i, j;
	
	cin >> w >> h >> n;
	rep(i, n) { cin >> x[i] >> y[i]; }
	x[n] = 1;
	y[n] = 1;
	x[n + 1] = w;
	y[n + 1] = h;
	n += 2;
	
	rep(i, n) rep(j, 1100) minCost[i][j] = INF;
	
	typedef tuple<int, int, int> T;
	priority_queue<T, vector<T>, greater<T> > que;
	
	minCost[n - 2][0] = 0;
	que.push(T(0, n - 2, 0));
	while (!que.empty()) {
		T now = que.top(); que.pop();
		int cst = get<0>(now);
		int pos = get<1>(now);
		int sz = get<2>(now) - 1;
		
		rep(j, n) {
			if (pos == j) continue;
			int lowSz = abs(x[j] - x[pos]) + abs(y[j] - y[pos]) - sz - 1;
			if (j == n - 2 && lowSz >= 0) continue;
			if (j == n - 1 && lowSz >= 0) continue;
			if (lowSz < 0) lowSz = 0;
			if (minCost[j][lowSz + 1] > cst + lowSz) {
				minCost[j][lowSz + 1] = cst + lowSz;
				que.push(T(cst + lowSz, j, lowSz + 1));
			}
		}
	}
	
	int ans = INF;
	rep(i, 1100) ans = min(ans, minCost[n - 1][i]);
	cout << ans << endl;
	return 0;
}
accuracy: 0.3978494623655914
time_ms: 20
memory_kb: 5224
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using ll  = long long;
#define int ll
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define erep(e,v) for(auto && e :v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9:(int)1e18)
#define EPS 0.0000000001
using namespace std;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
template<class T> void join(T a){for(auto itr :a){if(itr != *a.begin())cout << " "; cout << itr;} }
using ld  = long double;
using pii = pair<int,int>;
using piii = pair<int,pii>;
int W,H;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
bool valid(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
signed main(){
    int N;
    cin >> W >> H >> N;
    vector<pii>v(N);
    erep(e,v) cin >> e.first >> e.second,--e.first,--e.second;
    v.push_back(pii(0,0));
    v.push_back(pii(W-1,H-1));
    int s = N,t = N+1;
    vector<vector<int>>edge(N+2,vector<int>(N+2,INF));
    vector<vector<int>>d(N+2,vector<int>(1000+ 100,INF));
    priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>que;
    
    rep(i,N+2)rep(j,N+2)edge[i][j] = abs(v[i].first - v[j].first) + abs(v[i].second - v[j].second);
    que.push({0,s,0});
    d[s][0] = 0;
    while(!que.empty()){
        auto top = que.top();
        int dist = top.front(),v = top[1],cost = top[2];
        que.pop();
        if (dist > d[v][cost])continue;
        rep(nv,edge[v].size()){
            if(nv == v)continue;
            int ncost = edge[v][nv] - cost -(v != s and nv != t);
            cmax(ncost,0LL);
            if(d[nv][ncost] > dist + ncost){
                d[nv][ncost] = dist + ncost;
                que.push({d[nv][ncost],nv,ncost});
            }
        }
    }
    cout << *min_element(all(d[t])) << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 7072
final_rank: 8

code_snippet: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

constexpr int MAX_N = 100, INF = 1<<30;

using P = std::pair<int, int>;
using State = std::pair<int, P>;

int W, H, N;
int x[MAX_N], y[MAX_N];
int d[MAX_N][1010];
std::priority_queue<State, std::vector<State>, std::greater<State> > pque;
int ans = INF;

int main()
{
  scanf( "%d%d%d", &W, &H, &N );
  rep( i, N )
    scanf( "%d%d", x+i, y+i ), --x[i], --y[i];

  std::fill( (int*)d, (int*)(d+N), INF );

  rep( i, N )
  {
    int cost = x[i]+y[i];
    d[i][cost] = cost;
    pque.push( State( d[i][cost], P( i, cost ) ) );
  }

  while( !pque.empty() )
  {
    State st = pque.top(); pque.pop();
    int dist = st.first, v = st.second.first, cost = st.second.second;

    if( d[v][cost] < dist )
      continue;

    rep( i, N )
    {
      int ncost = std::max( 0, abs(x[i]-x[v])+abs(y[i]-y[v])-cost-1 );

      if( d[i][ncost] > d[v][cost]+ncost )
      {
        d[i][ncost] = d[v][cost]+ncost;
        pque.push( State( d[i][ncost], P( i, ncost ) ) );
      }
    }
  }

  rep( i, N ) rep( j, W+H+1 )
    ans = std::min( ans, d[i][j]+std::max(0,abs(W-1-x[i])+abs(H-1-y[i])-j) );

  printf( "%d\n", ans );
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3720
final_rank: 1

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


struct Data{
	int row,col;
};

struct Info{
	Info(){
		index = brightness = sum_cost = 0;
	}

	Info(int arg_index,int arg_brightness,int arg_sum_cost){
		index = arg_index;
		brightness = arg_brightness;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{
		return sum_cost > arg.sum_cost;
	}
	int index,brightness,sum_cost;
};


int N;
int W,H;
int min_cost[101][1000];
Data data[100];

int main(){

	scanf("%d %d %d",&W,&H,&N);

	for(int i = 0; i < N; i++){
		scanf("%d %d",&data[i].col,&data[i].row);
		data[i].col--;
		data[i].row--;
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k <= H+W-2; k++)min_cost[i][k] = BIG_NUM;
	}

	priority_queue<Info> Q;
	for(int i = 0; i < N; i++){
		min_cost[i][data[i].row+data[i].col] = data[i].row+data[i].col;
		Q.push(Info(i,data[i].row+data[i].col,data[i].row+data[i].col));
	}

	int dist,next_Brightness,next_cost;

	Info info;

	while(!Q.empty()){

		info = Q.top();
		Q.pop();

		if(info.brightness < H+W-2 && min_cost[info.index][info.brightness+1] > info.sum_cost+1){
			min_cost[info.index][info.brightness+1] = info.sum_cost+1;
			Q.push(Info(info.index,info.brightness+1,info.sum_cost+1));
		}

		for(int i = 0; i < N; i++){
			if(i == info.index)continue;

			dist = abs(data[info.index].row-data[i].row)+abs(data[info.index].col-data[i].col);

			if(dist <= info.brightness+1){
				next_Brightness = 0;
				next_cost = info.sum_cost;
			}else{
				next_Brightness = dist-(info.brightness+1);
				next_cost = info.sum_cost+next_Brightness;
			}

			if(min_cost[i][next_Brightness] > next_cost){
				min_cost[i][next_Brightness] = next_cost;
				Q.push(Info(i,next_Brightness,next_cost));
			}
		}
	}

	int ans = BIG_NUM;

	for(int i = 0; i < N; i++){
		dist = abs((H-1)-data[i].row)+abs((W-1)-data[i].col);
		for(int k = dist; k <= H+W-2; k++){
			ans = min(ans,min_cost[i][k]);
		}
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4192
final_rank: 11

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


struct Data{
	int row,col;
};

struct Info{
	Info(){
		index = brightness = sum_cost = 0;
	}

	Info(int arg_index,int arg_brightness,int arg_sum_cost){
		index = arg_index;
		brightness = arg_brightness;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{ //総コストの昇順(PQ)
		return sum_cost > arg.sum_cost;
	}
	int index,brightness,sum_cost;
};


int N;
int W,H;
int min_cost[101][1000];
Data data[100];

int main(){

	scanf("%d %d %d",&W,&H,&N);

	for(int i = 0; i < N; i++){
		scanf("%d %d",&data[i].col,&data[i].row);
		data[i].col--;
		data[i].row--;
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k <= H+W-2; k++)min_cost[i][k] = BIG_NUM; //min_cost[何番目の街灯か][明るさ] = 最小到達コスト
	}

	//★最初に訪れる街灯を全探索★
	priority_queue<Info> Q;
	for(int i = 0; i < N; i++){
		min_cost[i][data[i].row+data[i].col] = data[i].row+data[i].col;
		Q.push(Info(i,data[i].row+data[i].col,data[i].row+data[i].col)); //左上まで光を引っ張ってくるイメージ
	}

	int dist,next_Brightness,next_cost;

	Info info;

	while(!Q.empty()){

		info = Q.top();
		Q.pop();

		for(int i = 0; i < N; i++){
			if(i == info.index)continue;

			dist = abs(data[info.index].row-data[i].row)+abs(data[info.index].col-data[i].col);

			if(dist <= info.brightness+1){ //次の街灯への距離-1まで全て照らせる場合
				next_Brightness = 0;
				next_cost = info.sum_cost;
			}else{
				next_Brightness = dist-(info.brightness+1); //★次の街灯から距離1のところまで照らして貰えば良い★
				next_cost = info.sum_cost+next_Brightness;
			}

			if(min_cost[i][next_Brightness] > next_cost){
				min_cost[i][next_Brightness] = next_cost;
				Q.push(Info(i,next_Brightness,next_cost));
			}
		}
	}

	int ans = BIG_NUM;

	//右下までの最小コストを求める
	for(int i = 0; i < N; i++){
		dist = abs((H-1)-data[i].row)+abs((W-1)-data[i].col);
		for(int k = dist; k <= H+W-2; k++){
			ans = min(ans,min_cost[i][k]);
		}
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 0.3978494623655914
time_ms: 20
memory_kb: 4056
final_rank: 17

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


struct Data{
	int row,col;
};

struct Info{
	Info(int arg_index,int arg_brightness,int arg_sum_cost){
		index = arg_index;
		brightness = arg_brightness;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{ //総コストの昇順(PQ)
		return sum_cost > arg.sum_cost;
	}
	int index,brightness,sum_cost;
};


int N;
int W,H;
int min_cost[101][1000];
Data data[100];

int main(){

	scanf("%d %d %d",&W,&H,&N);

	for(int i = 0; i < N; i++){
		scanf("%d %d",&data[i].col,&data[i].row);
		data[i].col--;
		data[i].row--;
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k <= H+W-2; k++)min_cost[i][k] = BIG_NUM; //min_cost[何番目の街灯か][明るさ] = 最小到達コスト
	}

	//★最初に訪れる街灯を全探索★
	priority_queue<Info> Q;
	for(int i = 0; i < N; i++){
		min_cost[i][data[i].row+data[i].col] = data[i].row+data[i].col;
		Q.push(Info(i,data[i].row+data[i].col,data[i].row+data[i].col)); //左上まで光を引っ張ってくるイメージ
	}

	int dist,next_Brightness,next_cost;

	while(!Q.empty()){

		for(int i = 0; i < N; i++){
			if(i == Q.top().index)continue;

			dist = abs(data[Q.top().index].row-data[i].row)+abs(data[Q.top().index].col-data[i].col);

			if(dist <= Q.top().brightness+1){ //次の街灯への距離-1まで全て照らせる場合
				next_Brightness = 0;
				next_cost = Q.top().sum_cost;
			}else{
				next_Brightness = dist-(Q.top().brightness+1); //★次の街灯から距離1のところまで照らして貰えば良い★
				next_cost = Q.top().sum_cost+next_Brightness;
			}

			if(min_cost[i][next_Brightness] > next_cost){
				min_cost[i][next_Brightness] = next_cost;
				Q.push(Info(i,next_Brightness,next_cost));
			}
		}
		Q.pop();
	}

	int ans = BIG_NUM;

	//右下までの最小コストを求める
	for(int i = 0; i < N; i++){
		dist = abs((H-1)-data[i].row)+abs((W-1)-data[i].col);
		for(int k = dist; k <= H+W-2; k++){
			ans = min(ans,min_cost[i][k]);
		}
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 0.3978494623655914
time_ms: 20
memory_kb: 4088
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;



struct aa {
	int id;
	int li;
	int cost;

	const bool operator<(const aa&r)const {
		return this->cost>r.cost;
	}
};
int main() {
	int W,H,N;cin>>W>>H>>N;

	vector<pair<int,int>>lights(N+1);

	lights[0]=make_pair(0,0);
	for (int i = 0; i < N; ++i) {
		int x,y;cin>>x>>y;
		lights[i+1]=make_pair(x-1,y-1);
	}
	lights.emplace_back(W-1,H-1);


	priority_queue<aa>que;
	vector<vector<int>>dp(N+2,vector<int>(1010,1e8));

	que.push(aa{ 0,0,0 });;
	dp[0][0]=0;

	while (!que.empty()) {
		aa atop(que.top());
		que.pop();

		const int now_id(atop.id);
		const int now_li(atop.li);
		const int now_cost=atop.cost;

		for (int next_id = 1; next_id < N + 2; ++next_id) {
			if(now_id==next_id)continue;
			if(now_id==0&&next_id==N+1)continue;

			int dis=abs(lights[now_id].first-lights[next_id].first)+abs(lights[now_id].second-lights[next_id].second);

			if (next_id != N + 1) {
				int next_li=max(1,dis-now_li+1);
				int next_cost=now_cost+(next_li-1);

				if (dp[next_id][next_li] > next_cost) {
					dp[next_id][next_li]=next_cost;
					que.push(aa{ next_id,next_li,next_cost });
				}
			}
			else {
				if (dis <= now_li-1) {
					dp[N+1][0]=min(dp[N+1][0],now_cost);

				}
				else {
					dp[N+1][0]=min(dp[N+1][0],now_cost+(dis-(now_li-1)));
				}
			}
		}
	}
	int ans=dp[N+1][0];
	cout<<ans<<endl;

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4252
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;

struct state {
  ll v, r, cost;
  bool operator>(const state s) const { return cost > s.cost; }
};

int main(void) {
  ll W, H, N;
  cin >> W >> H >> N;
  vector<ll> X(N), Y(N);
  REP(i, 0, N) cin >> X[i] >> Y[i];

  const ll INF = 1LL << 60;
  vector<vector<ll> > dp(N, vector<ll>(1000, INF));
  priority_queue<state, vector<state>, greater<state> > q;
  REP(i, 0, N) {
    ll dist = (X[i] - 1) + (Y[i] - 1);
    q.push((state) { i, dist, dist });
  }
  ll ans = INF;
  while(!q.empty()) {
    ll v = q.top().v, r = q.top().r, cost = q.top().cost;
    // cout << v << " " << r << " " << cost << endl;
    q.pop();
    if(dp[v][r] <= cost) continue;
    dp[v][r] = cost;
    ans = min(ans, cost + max((W - X[v]) + (H - Y[v]) - r, 0LL));
    REP(nv, 0, N) if(nv != v) {
      ll dist = abs(X[nv] - X[v]) + abs(Y[nv] - Y[v]);
      ll nr = max(dist - r - 1, 0LL), ncost = cost + max(dist - r - 1, 0LL);
      if(dp[nv][nr] > ncost) q.push((state) { nv, nr, ncost });
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 530
memory_kb: 52932
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#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 srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int h,w,n;
vi x,y;

int dist(int a,int b,int c,int d)
{
    return abs(a-c)+abs(b-d);
}

template<typename T> class Dijkstra {
public:
	vector<T> d;
	int V;
	using pti = pair<T,int>;
	Dijkstra(int node_size){
		V = node_size;
	    d.resize(V,numeric_limits<T>::max());
	}
	void solve(){
		priority_queue<pti,vector<pti>,greater<pti> > que;
		rep(i,n){
            int dir = dist(x[i],y[i],0,0);
            d[i*1001+dir] = dir;
            // cout << i << " " << dir << " " << dir << "\n";
            que.push(pti(dir,i*1001+dir));
        }
		while(!que.empty()){
			pti p = que.top();
			que.pop();
			if(d[p.second] < p.first) continue;
            int v = p.second / 1001,cs = p.second % 1001;
			rep(i,n){
                int val = max(dist(x[v],y[v],x[i],y[i])-(cs+1),0);
                if(i != v && d[i*1001+val] > val+p.first){
                    d[i*1001+val] = val+p.first;
                    que.push(pti(d[i*1001+val],i*1001+val));
                }
            }
		}
	}
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> w >> h >> n;
    x.resize(n),y.resize(n);
    rep(i,n){
        cin >> x[i] >> y[i];
        --x[i],--y[i];
    }
    Dijkstra<int> dj(n*1001);
    dj.solve();
    int ans = INT_MAX;
    rep(i,n){
        rep(j,1001){
            if(dj.d[i*1001+j] != INT_MAX){
                cmn(ans,dj.d[i*1001+j]+max(dist(x[i],y[i],w-1,h-1)-j,0));
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3900
final_rank: 6

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

typedef pair<int,int>P;
typedef pair<int,P>P2;

int dp[111][1111],H,W,N,ans=1e9;
int x[111],y[111],man[111][111];

int main(){

  for(int i=0;i<111;i++)
    for(int j=0;j<1111;j++)dp[i][j]=1e8;

  x[0]=y[0]=1;

  cin>>W>>H>>N;N++;
  for(int i=1;i<N;i++)cin>>x[i]>>y[i];
  x[N]=W;
  y[N]=H; N++;

  for(int i=0;i<N;i++){
    for(int j=i;j<N;j++){
      man[i][j]=man[j][i]=(abs(x[i]-x[j])+abs(y[i]-y[j]));
    }
  }
  
  priority_queue<P2,vector<P2>,greater<P2> >q;
  q.push(P2(0,P(0,0)));
  dp[0][0]=0;

  while(!q.empty()){
    P2 p=q.top();q.pop();
    int cost = p.first;
    int now = p.second.first;
    int ran = p.second.second;
    if(now==N-1){
      cout<<cost<<endl;
      return 0;
    }
    if(dp[now][ran]<cost)continue;
    for(int i=0;i<N;i++)if(now!=i){
      int next;
      if(now==0)next=max(0,man[i][now]-ran);
      else if(i==N-1)next=max(0,man[i][now]-ran);
      else next=max(0,man[i][now]-ran-1);
      int cost2 = cost+next;     
      if(cost2>=dp[i][next])continue;
      dp[i][next]=cost2;
      q.push(P2(cost2,P(i,next)));
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 4336
final_rank: 3

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

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int x[105], y[105];
int d[105][1010];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int w, h, n;
  cin >> w >> h >> n;
  REP(i, n) {
    cin >> x[i] >> y[i];
    x[i]--, y[i]--;
  }

  REP(i, 105) REP(j, 1010) d[i][j] = LLINF;
  priority_queue<VI, VVI, greater<VI>> que;
  // スタート地点から街灯iに向かう
  REP(i, n) {
    int cost = x[i]+y[i];
    d[i][cost] = cost;
    que.push({cost, i, cost});
  }

  while(que.size()) {
    VI v = que.top(); que.pop();
    int dist = v[0], idx = v[1], cost = v[2];
    if(dist > d[idx][cost]) continue;
    REP(i, n) {
      if(idx == i) continue;
      int n_cost = max(0LL, abs(x[idx]-x[i]) + abs(y[idx]-y[i]) - cost - 1);
      if(d[i][n_cost] > d[idx][cost] + n_cost) {
        d[i][n_cost] = d[idx][cost] + n_cost;
        que.push({d[i][n_cost], i, n_cost});
      }
    }
  }

  int ans = LLINF;
  REP(i, n) REP(j, 1010) {
    chmin(ans, d[i][j] + max(0LL, abs(w-1-x[i]) + abs(h-1-y[i]) - j));
  }
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6724
final_rank: 9

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

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int x[105], y[105];
int d[105][1010];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int w, h, n;
  cin >> w >> h >> n;
  bool st_flag = false, en_flag = false;
  REP(i, n) {
    cin >> x[i] >> y[i];
    x[i]--, y[i]--;
    if(x[i] == 0 && y[i] == 0) st_flag = true;
    if(x[i] == w-1 && y[i] == h-1) en_flag = true;
  }

  REP(i, 105) REP(j, 1010) d[i][j] = LLINF;
  priority_queue<VI, VVI, greater<VI>> que;
  REP(i, n) {
    // スタート地点から街灯iに向かうとき
    int cost = x[i]+y[i];
    d[i][cost] = cost;
    que.push({cost, i, cost});
  }

  while(que.size()) {
    VI v = que.top(); que.pop();
    // cout << v << endl;
    int dist = v[0], idx = v[1], cost = v[2];
    if(dist > d[idx][cost]) continue;
    REP(i, n) {
      if(idx == i) continue;
      int n_cost = max(0LL, abs(x[idx]-x[i]) + abs(y[idx]-y[i]) - cost - 1);
      if(d[i][n_cost] > d[idx][cost] + n_cost) {
        d[i][n_cost] = d[idx][cost] + n_cost;
        que.push({d[i][n_cost], i, n_cost});
      }
    }
  }

  int ans = LLINF;
  REP(i, n) REP(j, 1010) {
    // cout << "i:" << i << " j:" << j << " " << d[i][j] << endl;
    chmin(ans, d[i][j] + max(0LL, abs(w-1-x[i]) + abs(h-1-y[i]) - (en_flag?1:0) - j));
  }
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6888
final_rank: 10

code_snippet: #include <bits/stdc++.h>
                                        #include<iostream>
                                        #include<cstdio>
                                        #include<vector>
                                        #include<queue>
                                        #include<map>
                                        #include<cstring>
                                        #include<string>
                                        #include <math.h>
                                        #include<algorithm>
                                    //    #include <boost/multiprecision/cpp_int.hpp>
                                        #include<functional>
                               #define int long long
                                        #define inf  1000000007
                                        #define pa pair<int,int>
                                        #define ll long long
                                        #define pal pair<double,double>
                                        #define ppap pair<pa,int>
                                  
                                        #define ssa pair<string,int>
                                        #define  mp make_pair
                                        #define  pb push_back
                                        #define EPS (1e-8)
                                        #define equals(a,b) (fabs((a)-(b))<EPS)
int dx[4]={0,-1,0,1};
int dy[4]={1,0,-1,0};
                                        using namespace std;
                               			class pa3{
                                        	public:
                                        	int x,y,z;
                                        	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                        	bool operator < (const pa3 &p) const{
                                        		if(x!=p.x) return x<p.x;
                                        		if(y!=p.y) return y<p.y;
                                        		 return z<p.z;
                                        		//return x != p.x ? x<p.x: y<p.y;
                                        	}
                               				bool operator > (const pa3 &p) const{
                                        		if(x!=p.x) return x>p.x;
                                        		if(y!=p.y) return y>p.y;
                                        		 return z>p.z;
                                        		//return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const pa3 &p) const{
                                        		return x==p.x && y==p.y && z==p.z;
                                        	}
                                        		bool operator != (const pa3 &p) const{
                                        			return !( x==p.x && y==p.y && z==p.z);
                                        	}
                                        
                                        };
                                        
                                        class pa4{
                                        	public:
                                        	double x;
                                        	int y,z,w;
                                        	pa4(double x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                        	bool operator < (const pa4 &p) const{
                                        		if(x!=p.x) return x<p.x;
                                        		if(y!=p.y) return y<p.y;
                                        		if(z!=p.z)return z<p.z;
                                        		return w<p.w;
                                        		//return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator > (const pa4 &p) const{
                                        		if(x!=p.x) return x>p.x;
                                        		if(y!=p.y) return y>p.y;
                                        		if(z!=p.z)return z>p.z;
                                        		return w>p.w;
                                        		//return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const pa4 &p) const{
                                        		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                        	}
                                        		
                                        
                                        };
                                        class pa2{
                                        	public:
                                        	int x,y;
                                        	pa2(int x=0,int y=0):x(x),y(y) {}
                                        	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                        	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                        	pa2 operator * (int a) {return pa2(x*a,y*a);}
                                        	bool operator < (const pa2 &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator > (const pa2 &p) const{
                                        		return x != p.x ? x>p.x: y>p.y;
                                        	}
                                        	bool operator == (const pa2 &p) const{
                                        		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                        	}
                                        	bool operator != (const pa2 &p) const{
                                        		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                        	}
                                        		
                                        
                                        };
                                        
                                               #define ppa pair<int,pas>
                                        class Point{
                                        	public:
                                        	double x,y;
                                        	Point(double x=0,double y=0):x(x),y(y) {}
                                        	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
                                        	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
                                        	Point operator * (double a) {return Point(x*a,y*a);}
                                        	Point operator / (double a) {return Point(x/a,y/a);}
                                        	double absv() {return sqrt(norm());}
                                        	double norm() {return x*x+y*y;}
                                        	bool operator < (const Point &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const Point &p) const{
                                        		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
                                        	}
                                        };
                                        typedef Point Vector;
                                 #define pl pair<int,pas>
                                        struct Segment{
                                        Point p1,p2;
                                        };
                                         double dot(Vector a,Vector b){
                                        	return a.x*b.x+a.y*b.y;
                                        }
                                        double cross(Vector a,Vector b){
                                        	return a.x*b.y-a.y*b.x;
                                        }
                                    
            bool parareru(Point a,Point b,Point c,Point d){
            //	if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
            	return abs(cross(a-b,d-c))<EPS;
            }
            double distance_ls_p(Point a, Point b, Point c) {
              if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
              if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
              return abs(cross(b-a, c-a)) / (b-a).absv();
            }
            bool is_intersected_ls(Segment a,Segment b) {
            	if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;
            	if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){
            //		cout<<"sss"<<endl;
            		if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
            		if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
            		if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
            		if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
            		return false;
            	}
              else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
            }
             
            double segment_dis(Segment a,Segment b){
            	if(is_intersected_ls(a,b))return 0;
            	double r=distance_ls_p(a.p1, a.p2, b.p1);
            	r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
            	return r;
            }
            Point intersection_ls(Segment a, Segment b) {
              Point ba = b.p2-b.p1;
              double d1 = abs(cross(ba, a.p1-b.p1));
              double d2 = abs(cross(ba, a.p2-b.p1));
              double t = d1 / (d1 + d2);
             
              return a.p1 + (a.p2-a.p1) * t;
            }
             
                            string itos( int i ) {
                            ostringstream s ;
                            s << i ;
                            return s.str() ;
                            }
                             
                            int gcd(int v,int b){
                            	if(v>b) return gcd(b,v);
                            	if(v==b) return b;
                            	if(b%v==0) return v;
                            	return gcd(v,b%v);
                            }
             
                            double distans(double x1,double y1,double x2,double y2){
                            	double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
                            	return sqrt(rr);
                            	
                            }
             /*               
            int pr[1000010];
            int inv[1000010];
            
            int beki(int wa,int rr,int warukazu){
            	if(rr==0) return 1%warukazu;
            	if(rr==1) return wa%warukazu;
            	if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
            	int zx=beki(wa,rr/2,warukazu);
            	return (zx*zx)%warukazu;
            }
double bekid(double w,int r){
	if(r==0) return 1.0;
	if(r==1) return w;
	if(r%2) return bekid(w,r-1)*w;
	double f=bekid(w,r/2);
	return f*f;
}

            
			int comb(int nn,int rr){
				int r=pr[nn]*inv[rr];
				r%=inf;
				r*=inv[nn-rr];
				r%=inf;
				return r;
			}
            
            
            void gya(int ert){
            	pr[0]=1;
            	for(int i=1;i<=ert;i++){
            		pr[i]=(pr[i-1]*i)%inf;
            	}
           	for(int i=0;i<=ert;i++) inv[i]=beki(pr[i],inf-2,inf);
            	
            }
            */ 
			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
             //sort(ve.begin(),ve.end(),greater<int>());
                                     //----------------kokomade tenpure------------
            //vector<double> ans(100000000),ans2(100000000)
/*
struct Segmin{
	//       1
	//   2        3
	// 4   5   6    7
	
	
	private:
	public:
	
	// (1<<15)=32768
	// 1<<16 = 65536
	// 1<<17 = 131072
	// 1<<18 = 262144
	
	int cor=(1<<17);
	
	vector<pa> vec;
	
	void shoki1(){
		
		vec.resize(2*cor+3, mp(0,0));
		for(int i=cor;i<2*cor;i++)vec[i].second=1;
	}
		void shoki2(){
			for(int i=cor-1;i>0;i--) {
				if(vec[2*i].first<vec[2*i+1].first) vec[i]=vec[2*i];
				else if(vec[2*i].first>vec[2*i+1].first)vec[i]=vec[2*i+1];
				else vec[i]=mp(vec[2*i].first,vec[2*i].second+vec[2*i+1].second);
			}
		}
		
	void updadd(int x,int w){
		//x ���ڂ� w���Z
		x+=cor;
		vec[x].first+=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x];
			else if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x+1];
			else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second);
		}
		
	}
	
	void updchan(int x,int w){
		//x���ڂ�w�ɕύX
		x+=cor;
		vec[x].first=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x];
			else if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x+1];
			else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second);
		}
		
	}
	// [a,b)
	// k-th node
	// k no kukanha [l,r)
	pa segmin(int a,int b,int k=1,int l=0,int r=-10){
		if(r<0)r=cor;
		
	//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
		
		if(a<=l && r<=b){
			return vec[k];
		}
		if(r<=a || b<=l){
			return mp((1ll<<31)-1,-1);
		}
		
		pa v1=segmin(a,b,k*2,l,(l+r)/2),v2=segmin(a,b,k*2+1,(l+r)/2,r);
		
		if(v1.first<v2.first)return v1;
		else if(v1.first>v2.first) return v2;
		else return mp(v1.first,v1.second+v2.second);
	}
	
};



struct Segmax{
	//       1
	//   2        3
	// 4   5   6    7
	
	
	private:
	public:
	
	// (1<<15)=32768
	// 1<<16 = 65536
	// 1<<17 = 131072
	// 1<<18 = 262144
	
	int cor=(1<<17);
	
	vector<pa> vec;
	
	void shoki1(){
		
		vec.resize(2*cor+3, mp(0,0));
		for(int i=cor;i<2*cor;i++)vec[i].second=1;
	}
		void shoki2(){
			for(int i=cor-1;i>0;i--) {
				if(vec[2*i].first>vec[2*i+1].first) vec[i]=vec[2*i];
				else if(vec[2*i].first<vec[2*i+1].first)vec[i]=vec[2*i+1];
				else vec[i]=mp(vec[2*i].first,vec[2*i].second+vec[2*i+1].second);
			}
		}
		
	void updadd(int x,int w){
		//x ���ڂ� w���Z
		x+=cor;
		vec[x].first+=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first>=vec[2*x+1].first) vec[x]=vec[2*x];
			else vec[x]=vec[2*x+1];
		}
		
	}
	
	void updchan(int x,int w){
		//x���ڂ�w�ɕύX
		x+=cor;
		vec[x].first=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first>vec[2*x+1].first) vec[x]=vec[2*x];
			else if(vec[2*x].first<vec[2*x+1].first) vec[x]=vec[2*x+1];
			else vec[x]=mp(vec[2*x].first,vec[2*x].second+vec[2*x+1].second);
		}
		
	}
	// [a,b)
	// k-th node
	// k no kukanha [l,r)
	pa segmax(int a,int b,int k=1,int l=0,int r=-10){
		if(r<0)r=cor;
		
	//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
		
		if(a<=l && r<=b){
			return vec[k];
		}
		if(r<=a || b<=l){
			return mp(-(1ll<<31),-1);
		}
		
		pa v1=segmax(a,b,k*2,l,(l+r)/2),v2=segmax(a,b,k*2+1,(l+r)/2,r);
		
		if(v1.first>v2.first)return v1;
		else if(v1.first<v2.first) return v2;
		else return mp(v1.first,v1.second+v2.second);
	}
	
};
*/
int w,h,n;
priority_queue<pa3,vector<pa3>,greater<pa3>> pq;   
bool sumi[200][2000]={0};
vector<pa3> G[110][1010];
int x[110],y[110];
signed main(){
cin>>w>>h>>n;
	for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
	
	for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++){
		int d=abs(x[i]-x[j])+abs(y[i]-y[j]);
		
		for(int k=0;k<d;k++){
				G[i][k].pb((pa3){j,d-k-1,d-k-1});
				G[j][k].pb((pa3){i,d-k-1,d-k-1});
		}
	}
	
	for(int  i=1;i<=n;i++){
		for(int j=0;j<=1001;j++){
			G[i][j].pb((pa3){i,j+1,1});
			G[i][j+1].pb((pa3){i,j,0});
		}
		int d=x[i]-1+y[i]-1;
	//	cout<<i<<" "<<d<<endl;
				G[0][0].pb((pa3){i,max(0ll,d),max(0ll,d)});
		d=w-x[i]+h-y[i];
	//	cout<<i<<" "<<d<<endl;
			G[i][max(0ll,d)].pb((pa3){0,1,0});
		
	}
	
		pq.push((pa3){0,0,0});
	
	while(pq.size()){
		pa3 z=pq.top();
		pq.pop();
		if(sumi[z.y][z.z])continue;
		sumi[z.y][z.z]=1;
	//	cout<<z.x<<" "<<z.y<<" "<<z.z<<endl;
		if(z.y==0 && z.z==1){
		cout<<z.x<<endl;
			return 0;
		}
		for(auto v:G[z.y][z.z]){
				pq.push((pa3){z.x+v.z,v.x,v.y});
		}
	
	
	}
	
	
	return 0;
	
        }
accuracy: 1
time_ms: 770
memory_kb: 183424
final_rank: 16

code_snippet: #include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
typedef pair<lint,pii>plpii;
#define rep(i,n)for(int i=0;i<(int)(n);++i)

const int N=1010;
const lint inf=1e16;

int main(){
  int w,h,n;
  cin>>w>>h>>n;
  int mi=1e8;
  vector<pii> pts(n+2);
  pts[0]=pii(1,1);
  pts[1]=pii(w,h);
  rep(i,n){
    int x,y;
    cin>>x>>y;
    pts[i+2]=pii(x,y);
  }
  priority_queue<plpii,vector<plpii>,greater<plpii> > que;
  vector<vector<lint> > dis(n+2,vector<lint>(N,inf));
  rep(i,n){
    int d=abs(pts[i+2].first-1)+abs(pts[i+2].second-1);
    que.push(plpii(d,pii(i+2,d)));
  }
  while(!que.empty()){
    plpii top=que.top();que.pop();
    lint d=top.first;
    pii xy=top.second;
    int x=xy.first,y=xy.second;
    if(dis[x][y]<=d)continue;
    dis[x][y]=d;
    rep(i,n+2){
      if(i==x||i==0)continue;
      int c=abs(pts[i].first-pts[x].first)+abs(pts[i].second-pts[x].second);
      c=max(0,i==1?c-y:(c-y-1));
      if(c>=N)continue;
      int nx=i;
      int ny=i==1?0:c;
      if(dis[nx][ny]<=d+c)continue;
      dis[nx][ny]=d+c+1;
      que.push(plpii(d+c,pii(nx,ny)));
    }
  }
  cout<<dis[1][0]<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 5792
final_rank: 12

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }


/*
<url:https://onlinejudge.u-aizu.ac.jp/services/room.html#RitsCamp18Day2/problems/E>
問題文============================================================

 W×H  の二次元のマス上にN個の街灯がある。
 がっちょ君は(1,1)からスタートして(W,H)に行きたい。
 がっちょ君は暗いところが怖いので、街灯により明るくなっているマスしか歩きたくない。
 最初、すべての街灯はその街灯のあるマスのみを明るくしている。
 そこで、がっちょ君は好きな街灯iに対してコストriを設定することにした。なお、コストを設定しない街灯があってもよい。
 街灯iはコストriを消費することにより、その街灯を中心にマンハッタン距離でri以内の範囲を明るくすることができる。ただしコストは正の整数とする。
 がっちょ君は上下左右のいずれかの方向に隣接するマスに移動する事ができる。
 がっちょ君はriの合計値を最小になるように設定することにした。そのときの合計値を求めよ。
 2つの地点 (a,b) と (c,d) 間のマンハッタン距離は |a−c|+|b−d| で表される。
 

 
=================================================================

解説=============================================================

 dist[i][j] := i頂点で明るさがjである地点への最小コストで
 
 拡張dijkstra
 
================================================================
*/

ll dist[105][505*505];
int main(void) {
	cin.tie(0); ios::sync_with_stdio(false);
    ll W,H,N; cin >> W >> H >> N;
    vector<ll> x(N+1),y(N+1);
    x[0] = y[0] = 1;
    for(int i = 1; i <= N;i++) cin >> x[i] >> y[i];
    vector<vector<ll>> G(N+1);
    
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N;j++){
            if(i == j) continue;
            //if(x[i] <= x[j]){// && y[i] <= y[j]){
                G[i].push_back(j);
            //}
        }
    }

    for(int i = 0; i < 105;i++)for(int j = 0; j < 505*505; j++) dist[i][j] = LINF;
    
    typedef pair<ll,pll> plpll;
 //   dist[0][0] = 0;
    priority_queue<plpll,vector<plpll>,greater<plpll>> pq;
    for(int i = 1; i <= N;i++){
        ll cost = abs(x[i]-x[0])+abs(y[i]-y[0]);
        pq.push({cost,{i,cost}});
        dist[i][cost] = cost;
    }
//    pq.push({0,{0,0}});
    while(pq.size()){
        auto p = pq.top(); pq.pop();
        ll d = p.first, n = p.second.first, r = p.second.second;
        if(dist[n][r] < d) continue;
        for(ll next : G[n]){
            ll mdis = abs(x[n]-x[next]) + abs(y[n]-y[next]);
            if(r >= mdis-1){
                if(dist[next][0] > dist[n][r]){
                    dist[next][0] = dist[n][r];
                    pq.push({dist[next][0],{next,0}});
                }
            }else{
                ll cost = mdis-r-1;
                if(dist[next][cost] > dist[n][r] + cost){
                    dist[next][cost] = dist[n][r] + cost;
                    pq.push({dist[next][cost],{next,cost}});
                }
            }
        }
    }
    ll ans = LINF;
    for(int i = 1; i <= N;i++){
        for(int j = 0; j < 505*505;j++){
            if(dist[i][j]==LINF)continue;
            ll mdis = abs(W-x[i])+abs(H-y[i]);
            ans = min(ans,dist[i][j]+max(mdis-j,0LL));
        }
    }
    cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 213924
final_rank: 15

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

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 60;

#define int long long
int W, H, N, X[100], Y[100];
int64 v[100][1005];

signed main() {
  srand((unsigned) time(NULL));
  cin >> W >> H >> N;

  for(int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }
  fill_n(*v, 100 * 1005, INF);

  if(rand() % 2) {
    fill_n(*v, 100 * 1001, INF);
    using Tuplis = tuple< int64, int, int >;

    priority_queue< Tuplis, vector< Tuplis >, greater< Tuplis > > que;


    for(int i = 0; i < N; i++) {
      v[i][abs(X[i] - 1) + abs(Y[i] - 1)] = abs(X[i] - 1) + abs(Y[i] - 1);
      que.emplace(abs(X[i] - 1) + abs(Y[i] - 1), abs(X[i] - 1) + abs(Y[i] - 1), i);
    }
    while(que.size()) {
      int64 cost;
      int l, j;
      tie(cost, l, j) = que.top();
      que.pop();
      if(v[j][l] < cost) continue;
      for(int k = 0; k < N; k++) {
        if(j == k) continue;
        int64 distance = abs(X[k] - X[j]) + abs(Y[k] - Y[j]);
        if(distance >= l) { // 距離のほうが遠い
          int64 add_cost = cost + (distance - l); // 追加で掛かるア
          if(add_cost < v[k][distance]) {
            v[k][distance] = add_cost;
            que.emplace(add_cost, distance, k);
          }
        } else { // 距離のほうが近い
          int64 add_cost = cost;
          if(add_cost < v[k][0]) {
            v[k][0] = add_cost;
            que.emplace(add_cost, 0, k);
          }
        }
      }


      if(l > 0) {
        if(v[j][l] < v[j][l - 1]) {
          v[j][l - 1] = v[j][l];
          que.emplace(v[j][l - 1], l - 1, j);
        }
      }
      if(l < 1000) {
        if(v[j][l] + 1 < v[j][l + 1]) {
          v[j][l + 1] = v[j][l] + 1;
          que.emplace(v[j][l + 1], l + 1, j);
        }
      }
    }
    int64 ret = INF;
    for(int i = 0; i < N; i++) {
      for(int j = 0; j <= 1000; j++) {
        int64 cost = abs(X[i] - W) + abs(Y[i] - H);
        ret = min(ret, v[i][j] + max(0LL, cost - j));
      }
    }
    cout << ret << endl;

  } else {
    using Tuplis = tuple< int64, int, int >;

    priority_queue< Tuplis, vector< Tuplis >, greater< Tuplis > > que;


    for(int i = 0; i < N; i++) {
      v[i][abs(X[i] - 1) + abs(Y[i] - 1)] = abs(X[i] - 1) + abs(Y[i] - 1) - 1;
      que.emplace(abs(X[i] - 1) + abs(Y[i] - 1) - 1, abs(X[i] - 1) + abs(Y[i] - 1), i);
    }
    while(que.size()) {
      int64 cost;
      int l, idx;
      tie(cost, l, idx) = que.top();
      que.pop();

      for(int k = 0; k < N; k++) {
        if(idx == k) continue;
        int64 distance = abs(X[k] - X[idx]) + abs(Y[k] - Y[idx]);

        int64 add_cost = cost + max(0LL, distance - l); // 追加で掛かるア
        if(add_cost < v[k][max(0LL, distance - l)]) {
          v[k][max(0LL, distance - l)] = add_cost;
          que.emplace(add_cost, max(0LL, distance - l), k);
        }

      }

      if(v[idx][l] + 1 < v[idx][l + 1]) {
        v[idx][l + 1] = v[idx][l] + 1;
        que.emplace(v[idx][l + 1], l + 1, idx);

      }
    }
    int64 ret = INF;
    for(int i = 0; i < N; i++) {
      for(int j = 0; j <= 1003; j++) {
        int64 cost = abs(X[i] - W) + abs(Y[i] - H);
        ret = min(ret, v[i][j] + max(0LL, cost - j));
      }
    }
    cout << ret << endl;
  }
}
accuracy: 0.043010752688172046
time_ms: 10
memory_kb: 3888
final_rank: 20