Problem:
L: スギ (Demon's Cedar)
月の瀬さんは悪魔らしいことをするため、街じゅうの樹木をスギの木に変えて、人々を花粉症で困らせることにした。
樹木は $N$ 種類存在する。$i$ 種類目の樹木の天界でのランクは $A_i$、魔界でのランクは $B_i$ である。また、$1$ 種類目の樹木はスギである。
月の瀬さんは、次の操作を何度でも行うことができる。
天界で、種類 $i$ の樹木を種類 $j$ に変える。これには $|A_i - A_j|$ の時間がかかる。
魔界で、種類 $i$ の樹木を種類 $j$ に変える。これには $|B_i - B_j|$ の時間がかかる。
天界と魔界の移動や、その他の行動に時間はかからない。
それぞれの樹木の種類について、スギの木に変えるためにかかる最短の時間を求めよ。
入力
1 行目に、$N$ が与えられる。
2 行目に、$A_1, A_2, A_3, \dots, A_N$ が空白区切りで与えられる。
3 行目に、$B_1, B_2, B_3, \dots, B_N$ が空白区切りで与えられる。
出力
$N$ 行のうち $i$ 行目には、$i$ 種類目の樹木をスギの木に変えるための時間の最小値を出力せよ。
制約
$N$ は $1$ 以上 $100 \ 000$ 以下の整数
$A_i, B_i$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数
入力例1
3
1 2 5
6 3 4
出力例1
0
1
2
樹木 1 は元からスギの木なので、コストはかかりません。
樹木 2 を天界で樹木 1 に変えると、コストは $|A_1 - A_2| = |1 - 2| = 1$ です。
樹木 3 を魔界で樹木 2 に変えたのち、天界で樹木 1 に変えるとコストは $|B_3 - B_2| + |A_2 - A_1| = |4 - 3| + |1 - 2| = 2$ です。
入力例2
5
1 10 5 6 9
2 3 4 7 8
出力例2
0
1
2
3
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
using len_type=int;
struct edge{
    int to;
    len_type length;
};

len_type dijkstra_movement(edge e,int from,len_type t){
    return t+e.length;
}


vector<len_type> dijkstra(vector<vector<edge>> &G,int start,len_type inf){
    int M=G.size();
    assert(M>start&&start>=0);
    vector<len_type> seen(M,inf);
    seen[start]=0;
    priority_queue<pair<len_type,int>,vector<pair<len_type,int>>,greater<pair<len_type,int>>> pq;
    pq.push({0,start});
    while(!pq.empty()){
        len_type time;
        int ver;
        tie(time,ver)=pq.top();
        pq.pop();
        if(seen[ver]<time) continue;
        for(edge x:G[ver]){
            len_type T=dijkstra_movement(x,ver,time);
            if(T<seen[x.to]){
                pq.push({T,x.to});
                seen[x.to]=T;
            }
        }
    }
    return seen;
}

void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N;
	cin>>N;
	vector<vector<edge>> G(N);
	auto f=[&]()->void{
		vector<int> A(N),order(N);
		rep(i,N) cin>>A[i],order[i]=i;
		sort(all(order),[&](int l,int r){
			return A[l]<A[r];
		});
		rep(i,N-1){
			int a=order[i],b=order[i+1];
			int c=A[b]-A[a];
			G[a].push_back({b,c});
			G[b].push_back({a,c});
		}
	};
	f(),f();
	auto seen=dijkstra(G,0,INF);
	rep(i,N) cout<<seen[i]<<"\n";
}
accuracy: 1
time_ms: 60
memory_kb: 12668
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
const long long INF = 100100100100100100;
#define pll pair<long long, long long>

int main(){
  
  int N;
  cin >> N;
  vector<pll> A(N), B(N);
  for(int i = 0; i < N; i++){
    cin >> A[i].first;
    A[i].second = i;
  }
  for(int i = 0; i < N; i++){
    cin >> B[i].first;
    B[i].second = i;
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  vector<vector<pll>> G(N);
  vector<long long> dist(N, INF);
  
  dist[0] = 0;
  for(int i = 0; i < N-1; i++){
    G[A[i].second].emplace_back(A[i+1].second, abs(A[i+1].first - A[i].first));
    G[B[i].second].emplace_back(B[i+1].second, abs(B[i+1].first - B[i].first));
    G[A[i+1].second].emplace_back(A[i].second, abs(A[i+1].first - A[i].first));
    G[B[i+1].second].emplace_back(B[i].second, abs(B[i+1].first - B[i].first));
    
  }
  
  priority_queue<pll, vector<pll>, greater<pll>> que;
  que.push({0, 0});
  
  while(!que.empty()){
    auto[cur, v] = que.top();
    que.pop();
    if(dist[v] != cur) continue;
    for(auto [nv, cost] : G[v]){
      if(dist[nv] > dist[v] + cost){
        dist[nv] = dist[v] + cost;
        que.push({dist[nv], nv});
      }
    }
  }
  
  for(int i = 0; i < N; i++){
    cout << dist[i] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 18832
final_rank: 10

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

struct edge{
    int src; // 有向辺の根本
    int to; // 有向辺の行き先
    long long w; // 同辺の重み
};
using Graph = vector<vector<edge>>;
 // 解にならないくらい大きい値。INFinity
// 今回は大丈夫だけど、何かが加えられることがあるのでオーバーフローしないよう注意
const long long INF = 1e12;

// priority_queueに渡す比較関数
// これが一番直感に反すると思うんだけど、
// デフォルトだとpriority_queueは"<"の比較を見てmax heapを作る
// ので、逆の比較を渡すとmin heapになる。
bool comp(edge l, edge r){
    return l.w > r.w;
}
// 復元もやってる
// 復元はprevsを逆から辿っていくと得られる。
pair<vector<long long>, vector<int>> dijkstra(const Graph& graph, int start){
    int n = graph.size();
    vector<int> prevs(n, -1); // 各頂点の直前に通る頂点を集める配列, 復元で使える
    vector<long long> dist(n, INF); // 解の配列。始点から各点までの最短距離を格納。
    dist[start] = 0; // 始点から始点の距離は0

    // 始点の前に通る頂点として-2を入れておく
    // prevsの初期値で始点から到達されないことを意味する-1とは区別する
    // (始点を場合分けすれば求まるはずなのでやってもやらなくてもよいが、今回はvisitedのかわりにも使うので要)
    priority_queue pq(comp, vector<edge>({{-2, start, 0}})); 
    while(!pq.empty()){
        auto [prev, cur, d] = pq.top();
        pq.pop();
        if(prevs[cur] != -1) continue; // visitedのかわり
        prevs[cur] = prev;
        for(auto next_edge:graph[cur]){
            int to = next_edge.to;
            int nx_weight = dist[cur] + next_edge.w; // priority_queueに入れるのは"始点からの距離"
            if(dist[to] < nx_weight) continue; // 確定済みの頂点はこれで十分キューに入らない
            dist[to] = nx_weight;
            pq.push({cur, to, nx_weight});
        }
    }
    return {dist, prevs};
}
// 始点からtまでの最短経路を復元する
// 始点から連結でない時はサイズ0の配列を返す
vector<int> build_path(vector<int>& prevs, int t){
    if(prevs[t] == -1) return {}; // 始点から連結でないことを意味する
    vector<int> res;
    res.push_back(t);
    while(prevs[t] != -2){
        t = prevs[t];
        res.push_back(t);
    }
    reverse(res.begin(), res.end());
    return res;
}

int main(){
    int n;
    cin >> n;
    vector<pair<int,int>> a(n), b(n);
    for(auto&& ai:a) cin >> ai.first;
    for(auto&& bi:b) cin >> bi.first;
    for(int i=0; i<n; ++i){
        a[i].second = i;
        b[i].second = i;
    }
    
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    Graph graph(n);
    for(int i=0; i<n-1; ++i){
        graph[a[i+1].second].push_back({a[i+1].second, a[i].second, a[i+1].first-a[i].first});
        graph[a[i].second].push_back({a[i].second, a[i+1].second, a[i+1].first-a[i].first});
        graph[b[i+1].second].push_back({b[i+1].second, b[i].second, b[i+1].first-b[i].first});
        graph[b[i].second].push_back({b[i].second, b[i+1].second, b[i+1].first-b[i].first});
    }
    auto dist = dijkstra(graph, 0).first;


     for(int i=0; i<n; ++i){
         cout << dist[i] << endl;
     }
}
accuracy: 1
time_ms: 150
memory_kb: 18928
final_rank: 11

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

template<typename T>
struct Dijkstra{
    const T inf=numeric_limits<T>::max();
    using P=pair<T,int>;
    int n;
    vector<vector<pair<int,T>>> g;
    vector<T> d;
    Dijkstra(int n):n(n),g(n),d(n){}
    void add_edge(int u,int v,T w){
        g[u].emplace_back(v,w);
        g[v].emplace_back(u,w);
    }
    vector<T> build(int s){
        for(int i=0;i<n;i++){
            d[i]=inf;
        }
        d[s]=0;
        priority_queue<P,vector<P>,greater<P>> pq;
        pq.emplace(d[s],s);
        while(pq.size()){
            P p=pq.top(); pq.pop();
            int v=p.second;
            if(d[v]<p.first)continue;
            for(auto &e:g[v]){
                int u=e.first; T c=e.second;
                if(d[u]>d[v]+c){
                    d[u]=d[v]+c;
                    pq.emplace(d[u],u);
                }
            }
        }
        return d;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    Dijkstra<ll> g(n);
    vector<pair<int,int>> a(n),b(n);
    for(int i=0;i<n;i++){
        cin >> a[i].first;
        a[i].second = i;
    }
    for(int i=0;i<n;i++){
        cin >> b[i].first;
        b[i].second = i;
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for(int i=0;i+1<n;i++){
        g.add_edge(a[i].second, a[i+1].second, a[i+1].first-a[i].first);
    }
    swap(a,b);
    for(int i=0;i+1<n;i++){
        g.add_edge(a[i].second, a[i+1].second, a[i+1].first-a[i].first);
    }
    auto r = g.build(0);
    for(int i=0;i<n;i++){
        cout << r[i] << "\n";
    }
}
accuracy: 1
time_ms: 60
memory_kb: 18664
final_rank: 6

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

using ll = long long;
static const ll INF = 1e18;

// ダイクストラ法 O(ElogV)
vector<ll> dijkstra(const vector<vector<pair<int, ll>>> &G, int s) {
  vector<ll> d(G.size(), INF);
  d[s] = 0;
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  pq.push(make_pair(0, s));
  while (!pq.empty()) {
    pair<ll, int> p = pq.top();
    pq.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); i++) {
      pair<int, ll> p2 = G[v][i];
      if (d[p2.first] > d[v] + p2.second) {
        d[p2.first] = d[v] + p2.second;
        pq.push(make_pair(d[p2.first], p2.first));
      }
    }
  }
  return d;
}

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

  int n;
  cin >> n;

  vector<pair<int, int>> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i).first;
    a.at(i).second = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> b.at(i).first;
    b.at(i).second = i;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  vector<vector<pair<int, ll>>> G(n);
  for (int i = 1; i < n; i++) {
    ll d = abs(a.at(i - 1).first - a.at(i).first);
    G[a.at(i - 1).second].emplace_back(make_pair(a.at(i).second, d));
    G[a.at(i).second].emplace_back(make_pair(a.at(i - 1).second, d));
    d = abs(b.at(i - 1).first - b.at(i).first);
    G[b.at(i - 1).second].emplace_back(make_pair(b.at(i).second, d));
    G[b.at(i).second].emplace_back(make_pair(b.at(i - 1).second, d));
  }

  vector<ll> dist = dijkstra(G, 0);
  
  for (int i = 0; i < n; i++) cout << dist.at(i) << '\n';
}
accuracy: 1
time_ms: 60
memory_kb: 17416
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n; cin >> n;
  vector<int> a(n), b(n);
  REP(i,n) cin >> a[i];
  REP(i,n) cin >> b[i];

  vector<pair<int, int>> vp1, vp2;
  REP(i,n) {
    vp1.push_back({a[i], i});
    vp2.push_back({b[i], i});
  }
  sort(all(vp1));
  sort(all(vp2));
  vector<vector<pair<int, ll>>> g(2*n);
  for(int i=0; i<n-1; i++) {
    int v = vp1[i].second, u = vp1[i+1].second;
    ll sa = abs(vp1[i].first-vp1[i+1].first);
    g[v].push_back({u, sa});
    g[u].push_back({v, sa});
  }
  for(int i=0; i<n-1; i++) {
    int v = vp2[i].second, u = vp2[i+1].second;
    ll sa = abs(vp2[i].first-vp2[i+1].first);
    g[v+n].push_back({u+n, sa});
    g[u+n].push_back({v+n, sa});
  }
  for(int i=0; i<n; i++) {
    g[i].push_back({i+n, 0});
    g[i+n].push_back({i, 0});
  }
  vector<ll> d(2*n,1e9);
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  pq.push({0, 0});
  d[0] = 0;
  while(pq.size()) {
    auto[c, v] = pq.top(); pq.pop();
    if(d[v] < c) continue;
    for(auto[u, cost]: g[v]) {
      if(d[u] > d[v] + cost) {
        d[u] = d[v] + cost;
        pq.push({d[u], u});
      }
    }
  }
  REP(i,n) cout << d[i] << '\n';
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 32564
final_rank: 14

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int main(){
    int n;
    cin >> n;
    vector<pii> va, vb;
    for(int i = 0; i < n; i++){
        int x;  cin >> x;
        va.push_back({x, i});
    }
    for(int i = 0; i < n; i++){
        int x;  cin >> x;
        vb.push_back({x, i});
    }
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end());
    vector<pii> v[n];
    for(int i = 0; i < n; i++){
        int aind = va[i].second, bind = vb[i].second;
        if(i > 0){
            v[aind].push_back({va[i-1].second, va[i].first-va[i-1].first});
            v[bind].push_back({vb[i-1].second, vb[i].first-vb[i-1].first});
        }
        if(i+1 < n){
            v[aind].push_back({va[i+1].second, va[i+1].first-va[i].first});
            v[bind].push_back({vb[i+1].second, vb[i+1].first-vb[i].first});
        }
    }
    vector<ll> dp(n, 1ll<<60);
    priority_queue<pair<ll,int>> pq;
    pq.push({-0, 0});
    dp[0] = 0;
    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        ll cost = -p.first;
        int ind = p.second;
        if(dp[ind] != cost) continue;
        for(pii P : v[ind]){
            if(dp[P.first] > dp[ind] + P.second){
                dp[P.first] = dp[ind] + P.second;
                pq.push({-dp[P.first], P.first});
            }
        } 
    }
    for(int i = 0; i < n; i++)  cout << dp[i] << endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 14156
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int N;
vector<pair<int,int> >G[1<<17];
main()
{
	cin>>N;
	vector<pair<int,int> >X(N),Y(N);
	for(int i=0;i<N;i++)
	{
		int a;cin>>a;
		X[i]=make_pair(a,i);
	}
	for(int i=0;i<N;i++)
	{
		int b;cin>>b;
		Y[i]=make_pair(b,i);
	}
	sort(X.begin(),X.end());
	sort(Y.begin(),Y.end());
	for(int i=1;i<N;i++)
	{
		int u=X[i-1].second,v=X[i].second;
		int c=X[i].first-X[i-1].first;
		G[u].push_back(make_pair(v,c));
		G[v].push_back(make_pair(u,c));
		u=Y[i-1].second,v=Y[i].second;
		c=Y[i].first-Y[i-1].first;
		G[u].push_back(make_pair(v,c));
		G[v].push_back(make_pair(u,c));
	}
	vector<int>d(N,2e9);
	d[0]=0;
	priority_queue<pair<int,int> >P;
	P.push(make_pair(0,0));
	while(!P.empty())
	{
		int u=P.top().second;
		int cost=-P.top().first;
		P.pop();
		if(d[u]<cost)continue;
		for(pair<int,int>p:G[u])
		{
			int v=p.first;
			int nxt=d[u]+p.second;
			if(d[v]>nxt)
			{
				d[v]=nxt;
				P.push(make_pair(-nxt,v));
			}
		}
	}
	for(int i=0;i<N;i++)cout<<d[i]<<endl;
}
accuracy: 1
time_ms: 180
memory_kb: 13768
final_rank: 8

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

int n;
struct edge{ int to,cost; };
const int N = 4e5+10;
vector< edge > G[N];

void dikstra(int s){
  typedef pair<int, int> P;
  const int INF = 1 << 30;

  priority_queue<P,vector<P>,greater<P>> que;
  vector<int> d(N,INF); //sからの最短距離
  d[s] = 0;
  que.push({0,s}); //{最短距離,頂点}

  while( !que.empty() ){
    P p = que.top(); que.pop();
    int v = p.second;
    if(d[v] < p.first) continue;
    for(auto e : G[v]){
      if(d[e.to] > d[v] + e.cost){
        d[e.to] = d[v] + e.cost;
        que.push( {d[e.to],e.to} );
      }
    }
  }

  for(int i = 0; i < n; i++){
    cout << d[i] << endl;
  }
}

signed main(){

  cin >> n;
  vector<pair<int,int>> v;
  for(int i = 0; i < n; i++){
    int a; cin >> a;
    v.push_back({a,i});
  }
  sort(v.begin(),v.end());
  for(int i = 0; i < n-1; i++){
    int cost = v[i+1].first - v[i].first;
    G[v[i].second].push_back({v[i+1].second,cost});
    G[v[i+1].second].push_back({v[i].second,cost});
  }

  v.clear();
  for(int i = 0; i < n; i++){
    int b; cin >> b;
    v.push_back({b,i+n});
  }
  sort(v.begin(),v.end());
  for(int i = 0; i < n-1; i++){
    int cost = v[i+1].first - v[i].first;
    G[v[i].second].push_back({v[i+1].second,cost});
    G[v[i+1].second].push_back({v[i].second,cost});
  }

  for(int i = 0; i < n; i++){
    G[i].push_back({i+n,0});
    G[i+n].push_back({i,0});
  }
  dikstra(0);

  return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 38108
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
typedef pair<int,int>P;
#define fi first
#define se second

int dp[100001];

vector<P>v[100001];

signed main(){

	r(i,100001)dp[i]=1e15;

	dp[0]=0;

	int n;
	cin>>n;
	int a[n],b[n];
	vector<P>A,B;
	r(i,n)cin>>a[i],A.push_back(P(a[i],i));
	r(i,n)cin>>b[i],B.push_back(P(b[i],i));

	sort(A.begin(),A.end());
	sort(B.begin(),B.end());

	r(i,n-1){
		int x=A[i].second;
		int y=A[i+1].second;
		v[x].push_back(P(y,abs(A[i].first-A[i+1].first)));
		v[y].push_back(P(x,abs(A[i].first-A[i+1].first)));
	}
	r(i,n-1){
		int x=B[i].second;
		int y=B[i+1].second;
		v[x].push_back(P(y,abs(B[i].first-B[i+1].first)));
		v[y].push_back(P(x,abs(B[i].first-B[i+1].first)));
	}

	priority_queue<P,vector<P>,greater<P> >q;
	q.push(P(0,0));

	while(!q.empty()){
		P p=q.top();q.pop();
		int now=p.se;
		int cost=p.fi;

		if(dp[now]<cost)continue;

		r(i,v[now].size()){
			int nex=v[now][i].fi;
			int ncos=v[now][i].se+cost;

			if(dp[nex]<=ncos)continue;

			dp[nex]=ncos;
			q.push(P(ncos,nex));
		}
	}
	r(i,n){
		cout<<dp[i]<<endl;
	}
}
accuracy: 1
time_ms: 200
memory_kb: 21376
final_rank: 16

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
 
 
#define NUM 100005
 
enum Which{
    A,
    B,
};
 
struct Info{
    Info(int arg_value,int arg_index){
        value = arg_value;
        index = arg_index;
    }
    bool operator<(const struct Info &arg) const{
 
        return value < arg.value;
    }
    int value,index;
};
 
struct State{
 
    State(Which arg_which,int arg_index,int arg_min_dist){
        which = arg_which;
        index = arg_index;
        min_dist = arg_min_dist;
    }
    bool operator<(const struct State &arg) const{
 
        return min_dist > arg.min_dist; //最短距離の昇順(PQ)
    }
 
    Which which;
    int index,min_dist;
 
};
 
int N;
int LOC[2][NUM];
int table[2][NUM],final[NUM];
vector<Info> info[2];
Which rev[2] = {B,A};
 
 
int main(){
 
    scanf("%d",&N);
 
    int tmp;
 
    for(int loop = 0; loop < N; loop++){
 
        scanf("%d",&tmp);
        info[A].push_back(Info(tmp,loop));
    }
 
    for(int loop = 0; loop < N; loop++){
 
        scanf("%d",&tmp);
        info[B].push_back(Info(tmp,loop));
    }
 
    sort(info[A].begin(),info[A].end());
    sort(info[B].begin(),info[B].end());
 
    //dataがどこにおかれているかを記録
    for(int i = 0; i < N; i++){
 
        LOC[A][info[A][i].index] = i;
        LOC[B][info[B][i].index] = i;
    }
 
    table[A][0] = 0;
    table[B][0] = 0;
    final[0] = 0;
 
    for(int i = 1; i < N; i++){
 
        table[A][i] = BIG_NUM;
        table[B][i] = BIG_NUM;
        final[i] = BIG_NUM;
    }
 
    priority_queue<State> Q;
 
    //始点に隣接するノードをQにpush
    for(int i = 0; i < N; i++){
 
        if(info[A][i].index == 0){
 
            if(i > 0){
 
                table[A][info[A][i-1].index] = abs(info[A][i].value-info[A][i-1].value);
                Q.push(State(A,info[A][i-1].index,abs(info[A][i].value-info[A][i-1].value)));
            }
            if(i < N-1){
 
                table[A][info[A][i+1].index] = abs(info[A][i].value-info[A][i+1].value);
                Q.push(State(A,info[A][i+1].index,abs(info[A][i].value-info[A][i+1].value)));
            }
            break;
        }
    }
 
    for(int i = 0; i < N; i++){
 
        if(info[B][i].index == 0){
 
            if(i > 0){
 
                table[B][info[B][i-1].index] = abs(info[B][i].value-info[B][i-1].value);
                Q.push(State(B,info[B][i-1].index,abs(info[B][i].value-info[B][i-1].value)));
            }
            if(i < N-1){
 
                table[B][info[B][i+1].index] = abs(info[B][i].value-info[B][i+1].value);
                Q.push(State(B,info[B][i+1].index,abs(info[B][i].value-info[B][i+1].value)));
            }
            break;
        }
    }
 
    int left_next,right_next,left_index,right_index;
 
    while(!Q.empty()){
 
        if(Q.top().min_dist > table[Q.top().which][Q.top().index]){
 
            Q.pop();
 
        }else{
 
            final[Q.top().index] = min(final[Q.top().index],table[Q.top().which][Q.top().index]); //最終最小値を更新
 
            //左右のノードを見る
            if(LOC[Q.top().which][Q.top().index] > 0){
 
                left_next = final[Q.top().index]+abs(info[Q.top().which][LOC[Q.top().which][Q.top().index]].value
                        -info[Q.top().which][LOC[Q.top().which][Q.top().index]-1].value);
                left_index = info[Q.top().which][LOC[Q.top().which][Q.top().index]-1].index;
 
                if(table[Q.top().which][left_index] > left_next){
 
                    table[Q.top().which][left_index] = left_next;
                    Q.push(State(Q.top().which,left_index,left_next));
                }
            }
            if(LOC[rev[Q.top().which]][Q.top().index] > 0){ //★★もう一方の経路も見る
 
                left_next = final[Q.top().index]+abs(info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]].value
                        -info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]-1].value);
                left_index = info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]-1].index;
 
                if(table[rev[Q.top().which]][left_index] > left_next){
 
                    table[rev[Q.top().which]][left_index] = left_next;
                    Q.push(State(rev[Q.top().which],left_index,left_next));
                }
            }
 
 
            if(LOC[Q.top().which][Q.top().index] < N-1){
 
                right_next = final[Q.top().index]+abs(info[Q.top().which][LOC[Q.top().which][Q.top().index]].value
                        -info[Q.top().which][LOC[Q.top().which][Q.top().index]+1].value);
                right_index = info[Q.top().which][LOC[Q.top().which][Q.top().index]+1].index;
 
                if(table[Q.top().which][right_index] > right_next){
 
                    table[Q.top().which][right_index] = right_next;
                    Q.push(State(Q.top().which,right_index,right_next));
                }
            }
 
            if(LOC[rev[Q.top().which]][Q.top().index] < N-1){ //★★もう一方の経路も見る
 
                right_next = final[Q.top().index]+abs(info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]].value
                        -info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]+1].value);
                right_index = info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]+1].index;
 
                if(table[rev[Q.top().which]][right_index] > right_next){
 
                    table[rev[Q.top().which]][right_index] = right_next;
                    Q.push(State(rev[Q.top().which],right_index,right_next));
                }
            }
 
            Q.pop();
        }
    }
 
    for(int i = 0; i < N; i++){
 
        printf("%d\n",final[i]);
    }
 
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8632
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

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

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

#define MAX_V 200000

struct edge{
	int to;
	long long int cost;
};

vector<edge> G[MAX_V];
long long int d[MAX_V];

// O(ElogV)

void shortest_path(int s, int V){
	
	priority_queue<P, vector<P>, greater<P> > que;
	
	for(int i = 0; i < V; i++){
		d[i] = INF;
	}
	d[s] = 0;
	
	que.push(P(0, s));
	
	while(!que.empty()){
		
		P p = que.top();
		que.pop();
		
		int v = p.second;
		
		if(d[v] < p.first){
			continue;
		}
		
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > e.cost + d[v]){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main(){
    int N;
    cin >> N;
    for(int loop = 0; loop < 2; loop++){
        vector<P> v;
        for(int i = 0; i < N; i++){
            long long int A;
            cin >> A;
            v.push_back(P(A, i));
        }
        sort(v.begin(), v.end());
        for(int i = 0; i < N - 1; i++){
            edge e;
            e.cost = v[i + 1].first - v[i].first;
            e.to = v[i + 1].second;
            G[v[i].second].push_back(e);
            e.to = v[i].second;
            G[v[i + 1].second].push_back(e);
        }
    }
    shortest_path(0, N);
    for(int i = 0; i < N; i++){
        cout << d[i] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 19508
final_rank: 15

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

vector<int>solve(const vector<vector<pair<int, int>>>&edges) {
	vector<int>memo(edges.size(),1e9);
	memo[0]=0;
	priority_queue<pair<int,int>>que;
	que.emplace(0,0);
	while (!que.empty()) {
		auto atop(que.top());
		que.pop();
		int now=atop.second;
		int now_cost=-atop.first;
		for (auto e : edges[now]) {
			int next=e.first;
			int next_cost=e.second+now_cost;
			if (memo[next] > next_cost) {
				memo[next]=next_cost;
				que.emplace(-next_cost,next);
			}
		}
	}
	return memo;
}
int main() {
	int N;cin>>N;
	vector<pair<int,int>>value_as,value_bs;
	for (int i = 0; i < N; ++i) {
		int a;cin>>a;
		value_as.push_back(make_pair(a,i));
	}
	for (int i = 0; i < N; ++i) {
		int b;cin>>b;
		value_bs.push_back(make_pair(b,i));
	}
	sort(value_as.begin(),value_as.end());
	sort(value_bs.begin(),value_bs.end());
	vector<vector<pair<int,int>>>edges(N);
	for (int i = 0; i < N - 1; ++i) {
		int l=value_as[i+1].second;
		int r=value_as[i].second;
		int cost=value_as[i+1].first-value_as[i].first;

		edges[l].push_back(make_pair(r,cost));
		edges[r].push_back(make_pair(l,cost));
	}
	for (int i = 0; i < N - 1; ++i) {
		int l = value_bs[i + 1].second;
		int r = value_bs[i].second;
		int cost = value_bs[i + 1].first - value_bs[i].first;

		edges[l].push_back(make_pair(r, cost));
		edges[r].push_back(make_pair(l, cost));
	}
	vector<int>answer=solve(edges);
	for(int i=0;i<N;++i)cout<<answer[i]<<endl;
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 13636
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;

struct edge{ int to,cost; };

const int INF = 1e9+7;

int main(){
    int n;
    cin >>n;

    vector<pi> a(n),b(n);
    rep(i,n){
        cin >>a[i].fi;
        a[i].se = i;
    }
    rep(i,n){
        cin >>b[i].fi;
        b[i].se = i;
    }

    sort(all(a));
    sort(all(b));

    vector<vector<edge>> G(n);
    rep(i,n-1){
        int u = a[i].se, v = a[i+1].se;
        int d = a[i+1].fi-a[i].fi;
        G[u].pb({v,d});
        G[v].pb({u,d});
    }
    rep(i,n-1){
        int u = b[i].se, v = b[i+1].se;
        int d = b[i+1].fi-b[i].fi;
        G[u].pb({v,d});
        G[v].pb({u,d});
    }

    vector<int> d(n,INF);
    d[0] = 0;
    priority_queue<pi, vector<pi>, greater<pi>> pq;
    pq.push({0,0});
    while(!pq.empty()){
        pi c = pq.top();
        pq.pop();
        int v = c.se;
        if(c.fi > d[v]) continue;
        for(const auto &e:G[v]){
            if(d[e.to] > d[v]+e.cost){
                d[e.to] = d[v]+e.cost;
                pq.push({d[e.to],e.to});
            }
        }
    }

    rep(i,n) cout << d[i] << "\n";
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 13448
final_rank: 7

code_snippet: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7,ohara=1e6;
const ll LINF=1e18;
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl

ll n,cnt,ans,a[ohara],b[ohara],c,d[ohara],tmp,tmpp,m,h,w,x,y,sum,pos,k;
ld doua;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz");
bool fl;
struct edge{ll to,cost;};
vector<edge> g[ohara];
pair<ll,ll> p[ohara];
pair<ll,ll> pp[ohara];
map<pair<ll,ll>,ll> mp;

//-------------------------↓↓↓↓↓↓------------------------

void dijkstra(ll s){
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>que;
    fill(d,d+n,LINF);
    d[s]=0;
    que.push(pair<ll,ll>(0LL,s));
    while(!que.empty()){
        pair<ll,ll> pp=que.top();que.pop();
        ll v=pp.second;
        if(d[v]<pp.first)continue;
        rep(i,Size(g[v])){
            edge e=g[v][i];
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                que.push(pair<ll,ll>(d[e.to],e.to));
            }
        }
    }
}

int main(void){
       cin.tie(0);
    ios::sync_with_stdio(false);

    cin>>n;
    rep(i,n)cin>>a[i];
    rep(i,n)p[i]=make_pair(a[i],i);
    rep(i,n)cin>>b[i];
    rep(i,n)pp[i]=make_pair(b[i],i);
    sort(p,p+n);
    sort(pp,pp+n);
    rep(i,n-1){
        //pos=min(abs(p[i].first-p[i+1].first),abs(pp[i].first-pp[i+1].first));
        if(mp[{p[i].second,p[i+1].second}]!=0)mp[{p[i].second,p[i+1].second}]=min(abs(p[i].first-p[i+1].first),mp[{p[i].second,p[i+1].second}]),mp[{p[i+1].second,p[i].second}]=mp[{p[i].second,p[i+1].second}];
        else mp[{p[i].second,p[i+1].second}]=abs(p[i].first-p[i+1].first),mp[{p[i+1].second,p[i].second}]=mp[{p[i].second,p[i+1].second}];
        if(mp[{pp[i].second,pp[i+1].second}]!=0)mp[{pp[i].second,pp[i+1].second}]=min(abs(pp[i].first-pp[i+1].first),mp[{pp[i].second,pp[i+1].second}]),mp[{pp[i+1].second,pp[i].second}]=mp[{pp[i].second,pp[i+1].second}];
        else mp[{pp[i].second,pp[i+1].second}]=abs(pp[i].first-pp[i+1].first),mp[{pp[i+1].second,pp[i].second}]=mp[{pp[i].second,pp[i+1].second}];
    }
    rep(i,n-1){
        pos=mp[{p[i].second,p[i+1].second}];
        g[p[i].second].push_back({p[i+1].second,pos});
        g[p[i+1].second].push_back({p[i].second,pos});
    }
    rep(i,n-1){
        pos=mp[{pp[i].second,pp[i+1].second}];
        g[pp[i].second].push_back({pp[i+1].second,pos});
        g[pp[i+1].second].push_back({pp[i].second,pos});
    }
    dijkstra(0);
    rep(i,n){
        Cout(d[i]);
    }
    return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 66928
final_rank: 20

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


#define NUM 100005

enum Which{
	A,
	B,
};

struct Info{
	Info(int arg_value,int arg_index){
		value = arg_value;
		index = arg_index;
	}
	bool operator<(const struct Info &arg) const{

		return value < arg.value;
	}
	int value,index;
};

struct State{

	State(Which arg_which,int arg_index,int arg_min_dist){
		which = arg_which;
		index = arg_index;
		min_dist = arg_min_dist;
	}
	bool operator<(const struct State &arg) const{

		return min_dist > arg.min_dist; //最短距離の昇順(PQ)
	}

	Which which;
	int index,min_dist;

};

int N;
int LOC[2][NUM];
int table[2][NUM],final[NUM];
vector<Info> info[2];
Which rev[2] = {B,A};


int main(){

	scanf("%d",&N);

	int tmp;

	for(int loop = 0; loop < N; loop++){

		scanf("%d",&tmp);
		info[A].push_back(Info(tmp,loop));
	}

	for(int loop = 0; loop < N; loop++){

		scanf("%d",&tmp);
		info[B].push_back(Info(tmp,loop));
	}

	sort(info[A].begin(),info[A].end());
	sort(info[B].begin(),info[B].end());

	//dataがどこにおかれているかを記録
	for(int i = 0; i < N; i++){

		LOC[A][info[A][i].index] = i;
		LOC[B][info[B][i].index] = i;
	}

	table[A][0] = 0;
	table[B][0] = 0;
	final[0] = 0;

	for(int i = 1; i < N; i++){

		table[A][i] = BIG_NUM;
		table[B][i] = BIG_NUM;
		final[i] = BIG_NUM;
	}

	priority_queue<State> Q;

	//始点に隣接するノードをQにpush
	for(int i = 0; i < N; i++){

		if(info[A][i].index == 0){

			if(i > 0){

				table[A][info[A][i-1].index] = abs(info[A][i].value-info[A][i-1].value);
				Q.push(State(A,info[A][i-1].index,abs(info[A][i].value-info[A][i-1].value)));
			}
			if(i < N-1){

				table[A][info[A][i+1].index] = abs(info[A][i].value-info[A][i+1].value);
				Q.push(State(A,info[A][i+1].index,abs(info[A][i].value-info[A][i+1].value)));
			}
			break;
		}
	}

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

		if(info[B][i].index == 0){

			if(i > 0){

				table[B][info[B][i-1].index] = abs(info[B][i].value-info[B][i-1].value);
				Q.push(State(B,info[B][i-1].index,abs(info[B][i].value-info[B][i-1].value)));
			}
			if(i < N-1){

				table[B][info[B][i+1].index] = abs(info[B][i].value-info[B][i+1].value);
				Q.push(State(B,info[B][i+1].index,abs(info[B][i].value-info[B][i+1].value)));
			}
			break;
		}
	}

	int left_next,right_next,left_index,right_index;

	while(!Q.empty()){

		if(Q.top().min_dist > table[Q.top().which][Q.top().index]){

			Q.pop();

		}else{

			final[Q.top().index] = min(final[Q.top().index],table[Q.top().which][Q.top().index]); //最終最小値を更新

			//左右のノードを見る
			if(LOC[Q.top().which][Q.top().index] > 0){

				left_next = final[Q.top().index]+abs(info[Q.top().which][LOC[Q.top().which][Q.top().index]].value
						-info[Q.top().which][LOC[Q.top().which][Q.top().index]-1].value);
				left_index = info[Q.top().which][LOC[Q.top().which][Q.top().index]-1].index;

				if(table[Q.top().which][left_index] > left_next){

					table[Q.top().which][left_index] = left_next;
					Q.push(State(Q.top().which,left_index,left_next));
				}
			}
			if(LOC[rev[Q.top().which]][Q.top().index] > 0){ //★★もう一方の経路も見る

				left_next = final[Q.top().index]+abs(info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]].value
						-info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]-1].value);
				left_index = info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]-1].index;

				if(table[rev[Q.top().which]][left_index] > left_next){

					table[rev[Q.top().which]][left_index] = left_next;
					Q.push(State(rev[Q.top().which],left_index,left_next));
				}
			}


			if(LOC[Q.top().which][Q.top().index] < N-1){

				right_next = final[Q.top().index]+abs(info[Q.top().which][LOC[Q.top().which][Q.top().index]].value
						-info[Q.top().which][LOC[Q.top().which][Q.top().index]+1].value);
				right_index = info[Q.top().which][LOC[Q.top().which][Q.top().index]+1].index;

				if(table[Q.top().which][right_index] > right_next){

					table[Q.top().which][right_index] = right_next;
					Q.push(State(Q.top().which,right_index,right_next));
				}
			}

			if(LOC[rev[Q.top().which]][Q.top().index] < N-1){ //★★もう一方の経路も見る

				right_next = final[Q.top().index]+abs(info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]].value
						-info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]+1].value);
				right_index = info[rev[Q.top().which]][LOC[rev[Q.top().which]][Q.top().index]+1].index;

				if(table[rev[Q.top().which]][right_index] > right_next){

					table[rev[Q.top().which]][right_index] = right_next;
					Q.push(State(rev[Q.top().which],right_index,right_next));
				}
			}

			Q.pop();
		}
	}

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

		printf("%d\n",final[i]);
	}

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8612
final_rank: 1

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;

using namespace std;
#define N 100010
ll d[N],n;
typedef pair<ll,ll> P;
P a[N],b[N];
vector<P> v[N];
void Dijkstra(){
    lol(i,N)d[i]=1e18;
    priority_queue<P,vector<P>,greater<P> >Q;
    Q.push(make_pair(0,0));
    while(!Q.empty()){
	ll x=Q.top().second;
	ll cost=Q.top().first;
	Q.pop();
	if(d[x]<=cost)continue;
	d[x]=cost;
	for(auto y:v[x]){
	    Q.push(make_pair(cost+y.second,y.first));
	}
    }
}
void ME(ll a,ll b,ll cost){
    v[a].push_back(make_pair(b,cost));
    v[b].push_back(make_pair(a,cost));
}
int main(){
    cin>>n;
    lol(i,n){cin>>a[i].first; a[i].second=i;}
    lol(i,n){cin>>b[i].first; b[i].second=i;}
    sort(a,a+n); sort(b,b+n);
    lol(i,n-1){
	ME(a[i].second,a[i+1].second,a[i+1].first-a[i].first);
	ME(b[i].second,b[i+1].second,b[i+1].first-b[i].first);
    }
    Dijkstra();
    lol(i,n)cout<<d[i]<<endl;
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 21840
final_rank: 18

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

using namespace std;

typedef long long ll;

signed main()
{
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    for(auto& x : a)
        cin >> x;
    for(auto& x : b)
        cin >> x;

    vector<pair<ll,ll>> p_a;
    vector<pair<ll,ll>> p_b;
    for(ll i = 0; i < n; ++i){
        p_a.emplace_back(a[i], i);
        p_b.emplace_back(b[i], i);
    }
    sort(p_a.begin(), p_a.end());;
    sort(p_b.begin(), p_b.end());;

    vector<vector<pair<ll,ll>>> edges(n);
    for(ll i = 0; i < n - 1; ++i){
        edges[p_a[i].second].emplace_back(p_a[i + 1].second, p_a[i + 1].first - p_a[i].first);
        edges[p_a[i + 1].second].emplace_back(p_a[i].second, p_a[i + 1].first - p_a[i].first);
        edges[p_b[i].second].emplace_back(p_b[i + 1].second, p_b[i + 1].first - p_b[i].first);
        edges[p_b[i + 1].second].emplace_back(p_b[i].second, p_b[i + 1].first - p_b[i].first);
    }

    vector<ll> range(n, 1e9 + 15);
    range[0] = 0;
    priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> que;
    que.emplace(0, 0);
    while(!que.empty()){
        ll pos = que.top().second;
        que.pop();
        for(auto& x : edges[pos]){
            if(range[x.first] > range[pos] + x.second){
                range[x.first] = range[pos] + x.second;
                que.emplace(range[x.first], x.first);
            }
        }
    }
    for(auto& x : range){
        cout << x << endl;
    }
}
accuracy: 1
time_ms: 210
memory_kb: 21056
final_rank: 17

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

using namespace std;
using i64 = int_fast64_t;
using db = long double;
using pii = pair<int, int>;
using pli = pair<int_fast64_t, int>;
using pdi = pair<double, int>;
template <class T> using mat = vector<vector<T>>;
template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

#define each(i,v) for (auto i = begin(v); i != end(v); ++i)
#define reach(i,v) for (auto i = rbegin(v); i != rend(v); ++i)
#define urep(i,s,t) for (int i = (s); i <= (t); ++i)
#define drep(i,s,t) for (int i = (s); i >= (t); --i)
#define rep(i,n) urep(i, 0, (n)-1)
#define rep1(i,n) urep(i, 1, (n))
#define rrep(i,n) drep(i, (n)-1, 0)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define fir first
#define sec second
#define front fro
#define back bac
#define vct vector
#define u_map unordered_map
#define u_set unordered_set
#define l_bnd lower_bound
#define u_bnd upper_bound
#define rsz resize
#define ers erase
#define emp emplace
#define emf emplace_front
#define emb emplace_back
#define pof pop_front
#define pob pop_back
#define mkp make_pair
#define mkt make_tuple
#define popcnt __builtin_popcount

struct setupper {
    setupper(uint_fast8_t prec) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(prec);
#ifdef Local
        auto print_atexit = []() { cerr << "Exec time : " << clock() / (double)CLOCKS_PER_SEC * 1000.0 << "ms\n"; };
        atexit(print_atexit);
#endif
    }
} setuppre_obj(10);

namespace std {
    template <class T> void hash_combine(size_t &seed, T const &key) {
        seed ^= hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    template <class T, class U> struct hash<pair<T,U>> {
        size_t operator()(pair<T,U> const &pr) const
        {
            size_t seed = 0;
            hash_combine(seed,pr.first);
            hash_combine(seed,pr.second);
            return seed;
        }
    };
    template <class Tup, size_t index = tuple_size<Tup>::value - 1> struct hashval_calc {
        static void apply(size_t& seed, Tup const& tup) {
            hashval_calc<Tup, index - 1>::apply(seed, tup);
            hash_combine(seed,get<index>(tup));
        }
    };
    template <class Tup> struct hashval_calc<Tup,0> {
        static void apply(size_t& seed, Tup const& tup) {
            hash_combine(seed,get<0>(tup));
        }
    };
    template <class ...T> struct hash<tuple<T...>> {
        size_t operator()(tuple<T...> const& tup) const
        {
            size_t seed = 0;
            hashval_calc<tuple<T...>>::apply(seed,tup);
            return seed;
        }
    };
}

template <class T, class U> ostream& operator << (ostream& s, pair<T,U> p) { return s << p.fir << " " << p.sec; }
template <class T> ostream& operator << (ostream& s, vct<T> v) { each(i,v) { if(i != begin(v)) s << " "; s << *i; } return s; }
#define dump(...) cerr << " [ " << __LINE__ << " : " << __FUNCTION__ << " ] " << #__VA_ARGS__ << " : ";\
dump_func(__VA_ARGS__)
template <class T> void dump_func(T x) { cerr << x << '\n'; }
template <class T,class ...Rest> void dump_func(T x, Rest ... rest) { cerr << x << ","; dump_func(rest...); }
void read() {}
template <class T, class ...Rest> void read(T &x, Rest &... rest) { cin >> x; read(rest...); }
template <class T> void write(T x) { cout << x << '\n'; }
template <class T, class ...Rest> void write(T x, Rest ... rest) { cout << x << ' '; write(rest...); }
void writeln() {}
template <class T, class ...Rest> void writeln(T x, Rest ... rest) { cout << x << '\n'; writeln(rest...); }
#define esc(...) writeln(__VA_ARGS__), exit(0)
const auto add = [](auto &x, auto y) { x += y; };
const auto mul = [](auto &x, auto y) { x *= y; };
const auto lam_min = [](auto x, auto y) { return min(x, y); };
const auto lam_max = [](auto x, auto y) { return max(x, y); };
const auto chmax = [](auto &m, auto x) { if(m < x){ m = x; return true; } return false; };
const auto chmin = [](auto &m, auto x) { if(m > x){ m = x; return true; } return false; };
bool bit(i64 n, uint8_t e) { return (n >> e) & 1; }
i64 mask(i64 n, uint8_t e) { return n & ((1 << e) - 1); }
int ilog(uint64_t x, uint64_t b = 2) { return x ? 1 + ilog(x / b,b) : -1; }
template <class F> i64 binsr(i64 ok, i64 ng, const F &fn) {
    while (abs(ok - ng) > 1) {
        i64 mid = (ok + ng) / 2;
        (fn(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class A, size_t N, class T> void init(A (&array)[N], const T &val) { fill((T*)array,(T*)(array + N),val); }
template <class T> T cmprs(T &v) {
    T tmp = v, ret = v;
    sort(all(tmp));
    tmp.erase(unique(all(tmp)), end(tmp));
    each(i,ret) *i = l_bnd(all(tmp),*i) - begin(tmp);
    return ret;
}

template <class T> constexpr T inf = numeric_limits<T>::max() / 2 - 1;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr long double gold = 1.618033988;
constexpr long double eps = 1e-15;
constexpr uint_fast64_t mod = 1e9 + 7;

int n;
pii a[1<<17],b[1<<17];
vct<pii> G[1<<17];

int main() {
    read(n);
    rep(i,n) read(a[i].fir);
    rep(i,n) read(b[i].fir);
    rep(i,n) a[i].sec=b[i].sec=i;
    sort(a,a+n);
    sort(b,b+n);
    rep(i,n-1) {
        int u=a[i].sec,v=a[i+1].sec;
        int c=a[i+1].fir-a[i].fir;
        G[u].emb(v,c);
        G[v].emb(u,c);
    }
    rep(i,n-1) {
        int u=b[i].sec,v=b[i+1].sec;
        int c=b[i+1].fir-b[i].fir;
        G[u].emb(v,c);
        G[v].emb(u,c);
    }
    minheap<pli> mh;
    i64 dist[1<<17];
    init(dist,inf<i64>);
    mh.emp(dist[0]=0,0);
    while(!mh.empty()) {
        pli t=mh.top();
        mh.pop();
        if(dist[t.sec]<t.fir) continue;
        for(pii u:G[t.sec]) {
            if(chmin(dist[u.fir],t.fir+u.sec)) {
                mh.emp(dist[u.fir],u.fir);
            }
        }
    }
    rep(i,n) {
        writeln(dist[i]);
    }
}
accuracy: 1
time_ms: 70
memory_kb: 15164
final_rank: 4

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

vector<pair<int, int> > graph[100010];

pair<int, int> p[100010];
pair<int, int> q[100010];

long long ans[100010] = {};

int main(){
  int n;
  cin >> n;

  int tmp;

  for(int i = 0;i < n;i++){
    cin >> tmp;
    p[i] = make_pair(tmp, i);
  }

  for(int i = 0;i < n;i++){
    cin >> tmp;
    q[i] = make_pair(tmp, i);
  }

  sort(p,p+n);
  sort(q,q+n);

  for(int i = 1;i < n;i++){
    graph[p[i].second].push_back(make_pair(p[i-1].second, p[i].first-p[i-1].first));
    graph[p[i-1].second].push_back(make_pair(p[i].second, p[i].first-p[i-1].first));
  }

  for(int i = 1;i < n;i++){
    graph[q[i].second].push_back(make_pair(q[i-1].second, q[i].first-q[i-1].first));
    graph[q[i-1].second].push_back(make_pair(q[i].second, q[i].first-q[i-1].first));
  }

  priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > que;

  ans[0] = 0;

  for(int i = 1;i < n;i++){
    ans[i] = LLONG_MAX;
  }

  que.push(make_pair(0, 0));

  while(!que.empty()){
    long long cost = que.top().first;
    int idx = que.top().second;
    que.pop();

    for(int i = 0;i < graph[idx].size();i++){
      int next = graph[idx][i].first;
      long long ncost = cost + graph[idx][i].second;
      if(ans[next] > ncost){
        ans[next] = ncost;
        que.push(make_pair(ncost, next));
      }
    }
  }

  for(int i = 0;i < n;i++){
    cout << ans[i] << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 14192
final_rank: 13