Problem:
Problem C: Manhattan Warp Machine 1
Problem
とある宇宙では、1次元の整数座標点上に星が存在し、宇宙人達はマンハッタンワープ装置を使い、星間を移動している。
このワープ装置には、
N
個のボタンが付いており、ボタン
i
を押すと、現在いる星からのマンハッタン距離が
d
i
である任意の星にコスト
c
i
でワープすることができる。
今、点0の星にいるある宇宙人が点
x
の星に行きたいと考えている。
点
x
の星に辿り着くまでの最小のコストを答えよ。
辿り着けない場合は-1を出力せよ。
1次元上の整数座標点には必ず星が存在する。
点
x
1
と点
x
2
間のマンハッタン距離は|
x
1
-
x
2
|で表される。
Input
N
x
d
1
c
1
...
d
N
c
N
入力は全て整数で与えられる。
1行目に
N
と行きたい星の座標
x
が空白区切りで与えられる。
続く
N
行に、移動可能なマンハッタン距離
d
i
とコスト
c
i
が1行ずつ空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
1 ≤
N
≤ 15
0 ≤
x
≤ 10
5
1 ≤
d
i
≤ 10
5
1 ≤
c
i
≤ 100
与えられるマンハッタン距離
d
i
は全て異なる。
Output
点
x
の星に辿り着くまでにかかる最小コストを1行に出力せよ。辿り着くことが不可能な場合は-1を出力せよ。
Sample Input 1
2 5
1 1
2 1
Sample Output 1
3
Sample Input 2
2 12
9 1
3 2
Sample Output 2
3
Sample Input 3
1 3
4 1
Sample Output 3
-1

Submitted solutions:

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

const int inf = 1e9;
const double INF = 1e12, EPS = 1e-9;

int d[20], c[20];
bool v[300000];

int main(){

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n, x;
    cin >> n >> x;
    for(int i = 0; i < n; i++) {
        cin >> d[i] >> c[i];
    }

    priority_queue<pair<int, int> > q;
    q.push(make_pair(0, 0));
    
    while(!q.empty()){
        pair<int, int> t = q.top();
        q.pop();
        
        int p = t.second;
        int co = -t.first;
        
        if(v[p]) continue;
        v[p] = true;
        
        if(p == x){
            cout << co << "\n";
            return 0;
        }
        
        for(int i = 0; i < n; i++){
            int np = p + d[i];
            if(np < 300000 && !v[np]){
                q.push(make_pair(-(co + c[i]), np));
            }

            np = p - d[i];
            if(np >= 0 && !v[np]){
                q.push(make_pair(-(co + c[i]), np));
            }
        }
    }
    
    cout << -1 << "\n";
    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 19408
final_rank: 8

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

using namespace std;

typedef long long ll;

const int inf = 1e9;
const double INF = 1e12, EPS = 1e-9;

int n, x, d[20], c[20];
bool v[300000];

int main(){
    // For faster I/O operations
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n >> x;
    for(int i = 0; i < n; i++) {
        cin >> d[i] >> c[i];
    }
    
    // Priority queue stores pairs of (negative cost, current position)
    // Note the space between the closing angle brackets
    priority_queue<pair<int, int> > q;
    q.push(make_pair(0, 0));
    
    while(!q.empty()){
        pair<int, int> top = q.top();
        q.pop();
        
        int cur = top.second;
        int cost = -top.first;
        
        if(v[cur]) continue;
        v[cur] = true;
        
        if(cur == x){
            cout << cost << "\n";
            return 0;
        }
        
        for(int i = 0; i < n; i++){
            // Move forward
            int nxt = cur + d[i];
            if(nxt < 300000 && !v[nxt]){
                q.push(make_pair(-(cost + c[i]), nxt));
            }
            
            // Move backward
            nxt = cur - d[i];
            if(nxt >= 0 && !v[nxt]){
                q.push(make_pair(-(cost + c[i]), nxt));
            }
        }
    }
    
    cout << -1 << "\n";
    return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 19472
final_rank: 9

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

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, x, d[20], c[20];
bool v[300000];

int main(){
	cin >> n >> x;
	rep(i, n) cin >> d[i] >> c[i];
	
	priority_queue<pi> q;
	q.push(pi(0, 0));
	while(!q.empty()){
		int cur = q.top().second, cost = -q.top().first; q.pop();
		if(v[cur]) continue;
		v[cur] = 1;
		if(cur == x){
			cout << cost << endl;
			return 0;
		}
		rep(i, n){
			int nxt = cur + d[i];
			if(nxt < 300000 && !v[nxt]) q.push(pi(-cost - c[i], nxt));
			
			nxt = cur - d[i];
			if(nxt >= 0 && !v[nxt]) q.push(pi(-cost - c[i], nxt));
		}
	}
	cout << -1 << endl;
	return 0;
}
accuracy: 1
time_ms: 520
memory_kb: 19480
final_rank: 10

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

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

class Dijkstra{
	using Point = pair<ll,ll>;
	vector<ll> dist;
public:
	void CalcShortestPath(int start,ConnectNodeInfo& connect){
		dist.resize(connect.size(),1LL<<60);
		dist[start] = 0;

		priority_queue<Point,vector<Point>,greater<Point>> Q;
		Q.emplace(0,start);
		while(!Q.empty()){
			auto p = Q.top();
			Q.pop();
			int v = p.second;
			if(dist[v]<p.first){
				continue;
			}
			for(auto e:connect[v]){
				if(dist[e.first]>dist[v]+e.second){
					dist[e.first] = dist[v]+e.second;
					Q.emplace(dist[e.first],e.first);
				}
			}
		}
	}
	ll GetDistance(int i){
		return dist[i];
	}
};

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

	auto N=in(),X=in();
	vector<pair<ll,ll>> P(N);
	REP(i,N){
		P[i].first=in();
		P[i].second=in();
	}
	
	ConnectNodeInfo connect(200100);
	REP(i,200100){
		REP(j,N){
			if(i-P[j].first>=0) connect.AddDirectionalConnection(i,i-P[j].first,P[j].second);
			if(i+P[j].first<200100) connect.AddDirectionalConnection(i,i+P[j].first,P[j].second);
		}
	}
	Dijkstra dij;
	dij.CalcShortestPath(0,connect);
	out(dij.GetDistance(X)>=(1ll<<60)?-1:dij.GetDistance(X));
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 117976
final_rank: 13

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

int n, x, cst[200001], d[15], c[15];
queue<int> que;
const int INF = 1000000001;

void dp(int here){
    if(!que.empty()) que.pop();
    if(cst[here] >= cst[x]) return;
    
    bool f[15] = {}, b[15] = {};
  
    for(int i = 0; i < n; i++){
        if(here - d[i] >= 0){
            if(cst[here - d[i]] > cst[here] + c[i]){
                cst[here - d[i]] = cst[here] + c[i];
                que.push(here - d[i]);
            }
        }
        if(here + d[i] <= 200000){
            if(cst[here + d[i]] > cst[here] + c[i]){
                cst[here + d[i]] = cst[here] + c[i];
                que.push(here + d[i]);
            }
        }
    }
    
    while(!que.empty())dp(que.front());
}

int main(){
    cin >> n >> x;
    int gc1 , i;
    for(i = 0; i < n; i++) cin >> d[i] >> c[i];
    
    gc1 = d[0];
    for(i = 1; i < n; i++){
        gc1 = gcd(gc1, d[i]);
    }
    if(x % gc1){
        cout << -1 << endl;
        return 0;
    }
    
    for(i = 0; i <= 200000; i++) cst[i] = INF;
    cst[0] = 0;
    
    dp(0);
    cout << cst[x] << endl;

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 43008
final_rank: 5

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

template <typename T>
struct dijkstra {
	struct edge {
		int to;
		T cost;
		edge() {}
		edge(int to, T cost) : to(to), cost(cost) {}
		bool operator<(const edge &e) const { return cost > e.cost; }
	};

	T inf() { return numeric_limits<T>::max() / 2; }

	vector<vector<edge>> G;
	vector<T> ds;
	vector<int> bs;
	dijkstra(int n) : G(n) {}

	void add_edge(int from, int to, T cost) { G[from].emplace_back(to, cost); }

	void build(int start) {
		int n = G.size();
		ds.assign(n, inf());
		bs.assign(n, -1);

		priority_queue<edge> Q;
		ds[start] = 0;
		Q.emplace(start, ds[start]);

		while ( !Q.empty() ) {
			auto p = Q.top();
			Q.pop();
			int v = p.to;
			if ( ds[v] < p.cost ) continue;
			for ( auto e : G[v] ) {
				if ( ds[e.to] > ds[v] + e.cost ) {
					ds[e.to] = ds[v] + e.cost;
					bs[e.to] = v;
					Q.emplace(e.to, ds[e.to]);
				}
			}
		}
	}

	T operator[](int k) { return ds[k]; }
};

int n,x;
vector<int> d,c;
constexpr int MAX = 5*1e5;
vector<bool> arrived(MAX,false);
void dfs(int idx){
	arrived[idx]=true;
	for(int i=0;i<n;i++){
		if(idx+d[i]<MAX && !arrived[idx+d[i]]){
			dfs(idx+d[i]);
		}
		if(idx-d[i]>=0 && !arrived[idx-d[i]]){
			dfs(idx-d[i]);
		}
	}
}

int main(){
	cin>>n>>x;
	d.resize(n);
	c.resize(n);
	for(int i=0;i<n;i++){
		cin>>d[i]>>c[i];
	}

	dfs(0);

	if(!arrived[x]){
		cout<<-1<<endl;
		return 0;
	}

	dijkstra<int> G(MAX);
	for(int i=0;i<MAX;i++){
		if(!arrived[i]) continue;

		for(int j=0;j<n;j++){
			if(i+d[j]<MAX) G.add_edge(i,i+d[j],c[j]);
			if(i-d[j]>=0) G.add_edge(i,i-d[j],c[j]);
		}
	}

	G.build(0);

	cout<<G[x]<<endl;
}
accuracy: 1
time_ms: 260
memory_kb: 180204
final_rank: 16

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

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

// dijkstra {{{
template < typename CostType >
class dijkstra {
  struct edge {
    int to;
    CostType cost;

    edge(int to_, CostType cost_) : to(to_), cost(cost_) {}
  };

  const CostType INF = numeric_limits<CostType>::max();

  int n;
  vector< vector<edge> > G;
  vector< CostType > ds;

  public:
  dijkstra(int n_) : n(n_), G(n_) {}

  void add_edge(int from, int to, CostType cost) {
    G[from].emplace_back(to, cost);
  }

  CostType inf() {
    return INF;
  }

  void build(int s) {
    using P = pair<CostType, int>;
    priority_queue< P, vector<P>, greater<P> > pq;

    ds.assign(n, INF);
    ds[s] = 0;
    pq.emplace(ds[s], s);

    while (!pq.empty()) {
      int v;
      CostType d;

      tie(d, v) = pq.top();
      pq.pop();

      if (ds[v] < d) continue;

      for (const edge &e : G[v]) {
        int u = e.to;
        if (ds[u] > ds[v] + e.cost) {
          ds[u] = ds[v] + e.cost;
          pq.emplace(ds[u], u);
        }
      }
    }
  }

  CostType dist(int to) {
    return ds[to];
  }

  CostType operator[](int k) {
    return dist(k);
  }
};
// }}}

void solve() {
  int n = input(), x = input();

  int V = x + 1 + 200000;
  dijkstra<int> G(V);
  range(i, 0, n) {
    int d = input(), c = input();
    range(t, d, V) {
      G.add_edge(t - d, t, c);
      G.add_edge(t, t - d, c);
    }
  }

  G.build(0);

  if (G[x] == G.inf()) {
    cout << -1 << endl;
  } else {
    cout << G[x] << endl;
  }
}

signed main() {
  solve();
}
accuracy: 1
time_ms: 230
memory_kb: 95268
final_rank: 11

code_snippet: #line 2 "/home/yuruhiya/programming/library/template/template.cpp"
#include <bits/stdc++.h>
#line 6 "/home/yuruhiya/programming/library/template/constants.cpp"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000000;
constexpr long long inf_ll = 1000000000000000000ll, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;
#line 7 "/home/yuruhiya/programming/library/template/Input.cpp"
using namespace std;

#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Scanner {
	static int gc() {
		return getchar_unlocked();
	}
	static char next_char() {
		char c;
		read(c);
		return c;
	}
	template <class T> static void read(T& v) {
		cin >> v;
	}
	static void read(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void read(bool& v) {
		v = next_char() != '0';
	}
	static void read(string& v) {
		v.clear();
		for (char c = next_char(); !isspace(c); c = gc()) v += c;
	}
	static void read(int& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(long long& v) {
		v = 0;
		bool neg = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc()) v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void read(double& v) {
		v = 0;
		double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	static void read(long double& v) {
		v = 0;
		long double dp = 1;
		bool neg = false, after_dp = false;
		char c = next_char();
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.') {
				after_dp = true;
			} else if (after_dp) {
				v += (c - '0') * (dp *= 0.1);
			} else {
				v = v * 10 + (c - '0');
			}
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void read(pair<T, U>& v) {
		read(v.first);
		read(v.second);
	}
	template <class T> static void read(vector<T>& v) {
		for (auto& e : v) read(e);
	}
	template <size_t N = 0, class T> static void read_tuple_impl(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			read(get<N>(v));
			read_tuple_impl<N + 1>(v);
		}
	}
	template <class... T> static void read(tuple<T...>& v) {
		read_tuple_impl(v);
	}
	struct ReadVectorHelper {
		size_t n;
		ReadVectorHelper(size_t _n) : n(_n) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			read(v);
			return v;
		}
	};
	struct Read2DVectorHelper {
		size_t n, m;
		Read2DVectorHelper(const pair<size_t, size_t>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			read(v);
			return v;
		}
	};

public:
	string read_line() const {
		string v;
		for (char c = gc(); c != '\n' && c != '\0'; c = gc()) v += c;
		return v;
	}
	template <class T> T read() const {
		T v;
		read(v);
		return v;
	}
	template <class T> vector<T> read_vector(size_t n) const {
		vector<T> a(n);
		read(a);
		return a;
	}
	template <class T> operator T() const {
		return read<T>();
	}
	int operator--(int) const {
		return read<int>() - 1;
	}
	ReadVectorHelper operator[](size_t n) const {
		return ReadVectorHelper(n);
	}
	Read2DVectorHelper operator[](const pair<size_t, size_t>& nm) const {
		return Read2DVectorHelper(nm);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		read(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail>
	struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void multiple_impl(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(read<V>());
			multiple_impl<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(size_t h) const {
		multiple_t<T...> result;
		while (h--) multiple_impl(result);
		return result;
	}
} in;
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)
#line 7 "/home/yuruhiya/programming/library/template/Output.cpp"
#include <charconv>
#line 10 "/home/yuruhiya/programming/library/template/Output.cpp"
using namespace std;

struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"),
    no_endl(" ", "");
class Printer {
	BoolStr B{Yes};
	DivStr D{spc};

public:
	void print(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void print(bool v) const {
		print(v ? B.t : B.f);
	}
	void print(vector<bool>::reference v) const {
		print(v ? B.t : B.f);
	}
	void print(char v) const {
		putchar_unlocked(v);
	}
	void print(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void print(double v) const {
		printf("%.20f", v);
	}
	void print(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void print(const T& v) const {
		cout << v;
	}
	template <class T, class U> void print(const pair<T, U>& v) const {
		print(v.first);
		print(D.d);
		print(v.second);
	}
	template <class InputIterater>
	void print_range(const InputIterater& begin, const InputIterater& end) const {
		for (InputIterater i = begin; i != end; ++i) {
			if (i != begin) print(D.d);
			print(*i);
		}
	}
	template <class T> void print(const vector<T>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T, size_t N> void print(const array<T, N>& v) const {
		print_range(v.begin(), v.end());
	}
	template <class T> void print(const vector<vector<T>>& v) const {
		for (size_t i = 0; i < v.size(); ++i) {
			if (i) print(D.l);
			print(v[i]);
		}
	}

	Printer() = default;
	Printer(const BoolStr& _boolstr, const DivStr& _divstr) : B(_boolstr), D(_divstr) {}
	Printer& operator()() {
		print(D.l);
		return *this;
	}
	template <class H> Printer& operator()(H&& h) {
		print(h);
		print(D.l);
		return *this;
	}
	template <class H, class... T> Printer& operator()(H&& h, T&&... t) {
		print(h);
		print(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class InputIterator>
	Printer& range(const InputIterator& begin, const InputIterator& end) {
		print_range(begin, end);
		print(D.l);
		return *this;
	}
	template <class T> Printer& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Printer& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Printer& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Printer& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Printer& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;
#line 3 "/home/yuruhiya/programming/library/template/Step.cpp"
using namespace std;

template <class T> struct Step {
	using value_type = T;

	class iterator {
		value_type a, b, c;

	public:
		constexpr iterator() : a(value_type()), b(value_type()), c(value_type()) {}
		constexpr iterator(value_type _b, value_type _c, value_type _s)
		    : a(_b), b(_c), c(_s) {}
		constexpr iterator& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr iterator operator++(int) {
			iterator tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const value_type& operator*() const {
			return a;
		}
		constexpr const value_type* operator->() const {
			return &a;
		}
		constexpr bool operator==(const iterator& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const iterator& i) const {
			return !(b == i.b);
		}
		constexpr value_type start() const {
			return a;
		}
		constexpr value_type size() const {
			return b;
		}
		constexpr value_type step() const {
			return c;
		}
	};
	constexpr Step(value_type b, value_type c, value_type s) : be(b, c, s) {}
	constexpr iterator begin() const {
		return be;
	}
	constexpr iterator end() const {
		return en;
	}
	constexpr value_type start() const {
		return be.start();
	}
	constexpr value_type size() const {
		return be.size();
	}
	constexpr value_type step() const {
		return be.step();
	}
	constexpr value_type sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<value_type>() const {
		return to_a();
	}
	auto to_a() const {
		vector<value_type> result;
		result.reserve(size());
		for (auto i : *this) {
			result.push_back(i);
		}
		return result;
	}

private:
	iterator be, en;
};
template <class T> constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}
#line 8 "/home/yuruhiya/programming/library/template/Ruby.cpp"
using namespace std;

template <class F> struct Callable {
	F func;
	Callable(const F& f) : func(f) {}
};
template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
	return c.func(v);
}

struct Sort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
		sort(begin(v), end(v));
		return v;
	}
} Sort;
struct SortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) < f(j); });
			return v;
		});
	}
} SortBy;
struct RSort_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(rbegin(v), rend(v), f);
			return v;
		});
	}
	template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
		sort(rbegin(v), rend(v));
		return v;
	}
} RSort;
struct RSortBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			sort(begin(v), end(v),
			     [&](const auto& i, const auto& j) { return f(i) > f(j); });
			return v;
		});
	}
} RSortBy;
struct Reverse_impl {
	template <class T> friend auto operator|(T v, const Reverse_impl& c) {
		reverse(begin(v), end(v));
		return v;
	}
} Reverse;
struct Unique_impl {
	template <class T> friend auto operator|(T v, const Unique_impl& c) {
		v.erase(unique(begin(v), end(v), end(v)));
		return v;
	}
} Unique;
struct Uniq_impl {
	template <class T> friend auto operator|(T v, const Uniq_impl& c) {
		sort(begin(v), end(v));
		v.erase(unique(begin(v), end(v)), end(v));
		return v;
	}
} Uniq;
struct Rotate_impl {
	auto operator()(int&& left) {
		return Callable([&](auto v) {
			int s = static_cast<int>(size(v));
			assert(-s <= left && left <= s);
			if (0 <= left) {
				rotate(begin(v), begin(v) + left, end(v));
			} else {
				rotate(begin(v), end(v) + left, end(v));
			}
			return v;
		});
	}
} Rotate;
struct Max_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *max_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Max_impl& c) {
		return *max_element(begin(v), end(v));
	}
} Max;
struct Min_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) { return *min_element(begin(v), end(v), f); });
	}
	template <class T> friend auto operator|(T v, const Min_impl& c) {
		return *min_element(begin(v), end(v));
	}
} Min;
struct MaxPos_impl {
	template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
		return max_element(begin(v), end(v)) - begin(v);
	}
} MaxPos;
struct MinPos_impl {
	template <class T> friend auto operator|(T v, const MinPos_impl& c) {
		return min_element(begin(v), end(v)) - begin(v);
	}
} MinPos;
struct MaxBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_it = begin(v);
			auto max_val = f(*max_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_it = it;
					max_val = val;
				}
			}
			return *max_it;
		});
	}
} MaxBy;
struct MinBy_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_it = begin(v);
			auto min_val = f(*min_it);
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_it = it;
					min_val = val;
				}
			}
			return *min_it;
		});
	}
} MinBy;
struct MaxOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto max_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); max_val < val) {
					max_val = val;
				}
			}
			return max_val;
		});
	}
} MaxOf;
struct MinOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			auto min_val = f(*begin(v));
			for (auto it = next(begin(v)); it != end(v); ++it) {
				if (auto val = f(*it); min_val > val) {
					min_val = val;
				}
			}
			return min_val;
		});
	}
} MinOf;
struct Count_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return count(begin(v), end(v), val); });
	}
} Count;
struct CountIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return count_if(begin(v), end(v), f); });
	}
} CountIf;
struct Index_impl {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(begin(v), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
	template <class V> auto operator()(const V& val, size_t i) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find(next(begin(v), i), end(v), val);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} Index;
struct IndexIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<int> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(result - begin(v)) : nullopt;
		});
	}
} IndexIf;
struct FindIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
			auto result = find_if(begin(v), end(v), f);
			return result != end(v) ? optional(*result) : nullopt;
		});
	}
} FindIf;
struct Sum_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			return accumulate(next(begin(v)), end(v), f(*begin(v)),
			                  [&](const auto& a, const auto& b) { return a + f(b); });
		});
	}
	template <class T> friend auto operator|(T v, const Sum_impl& c) {
		return accumulate(begin(v), end(v), typename T::value_type{});
	}
} Sum;
struct Includes {
	template <class V> auto operator()(const V& val) {
		return Callable([&](auto v) { return find(begin(v), end(v), val) != end(v); });
	}
} Includes;
struct IncludesIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) { return find_if(begin(v), end(v), f) != end(v); });
	}
} IncludesIf;
struct RemoveIf_impl {
	template <class F> auto operator()(const F& f) {
		return Callable([&](auto v) {
			v.erase(remove_if(begin(v), end(v), f), end(v));
			return v;
		});
	}
} RemoveIf;
struct Each_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				f(i);
			}
		});
	}
} Each;
struct EachConsPair_impl {
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, EachConsPair_impl& c) {
		vector<pair<value_type, value_type>> result;
		if (size(v) >= 2) {
			result.reserve(size(v) - 1);
			for (size_t i = 0; i < size(v) - 1; ++i) {
				result.emplace_back(v[i], v[i + 1]);
			}
		}
		return result;
	}
} EachConsPair;
struct Select_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using value_type = typename decltype(v)::value_type;
			vector<value_type> result;
			for (const auto& i : v) {
				if (f(i)) result.push_back(i);
			}
			return result;
		});
	}
} Select;
struct Map_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
			vector<result_type> result;
			result.reserve(size(v));
			for (const auto& i : v) {
				result.push_back(f(i));
			}
			return result;
		});
	}
} Map;
struct Indexed_impl {
	template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
		using value_type = typename T::value_type;
		vector<pair<value_type, int>> result;
		result.reserve(size(v));
		int index = 0;
		for (const auto& i : v) {
			result.emplace_back(i, index++);
		}
		return result;
	}
} Indexed;
struct AllOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (!f(i)) return false;
			}
			return true;
		});
	}
} AllOf;
struct AnyOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return true;
			}
			return false;
		});
	}
} AnyOf;
struct NoneOf_impl {
	template <class F> auto operator()(F&& f) {
		return Callable([&](auto v) {
			for (const auto& i : v) {
				if (f(i)) return false;
			}
			return true;
		});
	}
} NoneOf;

struct Tally_impl {
	template <class F> auto operator()(size_t max_val) {
		return Callable([&](auto v) {
			vector<size_t> result(max_val);
			for (const auto& i : v) {
				result[static_cast<size_t>(i)]++;
			}
			return result;
		});
	}
	template <class T, class value_type = typename T::value_type>
	friend auto operator|(const T& v, Tally_impl& c) {
		map<value_type, size_t> result;
		for (const auto& i : v) {
			result[i]++;
		}
		return result;
	}
} Tally;

template <class T> auto operator*(const vector<T>& a, size_t n) {
	T result;
	for (size_t i = 0; i < n; ++i) {
		result.insert(result.end(), a.begin(), a.end());
	}
	return result;
}
auto operator*(string a, size_t n) {
	string result;
	for (size_t i = 0; i < n; ++i) {
		result += a;
	}
	return result;
}
template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T> auto& operator<<(string& a, const T& b) {
	a.insert(a.end(), all(b));
	return a;
}
template <class T, class U> auto operator+(vector<T> a, const U& b) {
	a << b;
	return a;
}
template <class T> auto operator+(string a, const T& b) {
	a << b;
	return a;
}
#line 7 "/home/yuruhiya/programming/library/template/functions.cpp"
using namespace std;

template <class T = long long> constexpr T TEN(size_t n) {
	T result = 1;
	for (size_t i = 0; i < n; ++i) result *= 10;
	return result;
}
template <class T, class U,
          enable_if_t<is_integral_v<T> && is_integral_v<U>, nullptr_t> = nullptr>
constexpr auto div_ceil(T n, U m) {
	return (n + m - 1) / m;
}
template <class T, class U> constexpr auto div_ceil2(T n, U m) {
	return div_ceil(n, m) * m;
}
template <class T> constexpr T triangle(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> constexpr T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T, class U> constexpr auto middle(const T& l, const U& r) {
	return l + (r - l) / 2;
}
template <class T, class U, class V>
constexpr bool in_range(const T& v, const U& lower, const V& upper) {
	return lower <= v && v < upper;
}
template <class T, enable_if_t<is_integral_v<T>, nullptr_t> = nullptr>
constexpr bool is_square(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> constexpr T BIT(int b) {
	return T(1) << b;
}
template <class T> constexpr int BIT(T x, int i) {
	return (x & (T(1) << i)) ? 1 : 0;
}
template <class T> constexpr int Sgn(T x) {
	return (0 < x) - (0 > x);
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Pow(T a, U n) {
	assert(n >= 0);
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result *= a;
			n--;
		} else {
			a *= a;
			n >>= 1;
		}
	}
	return result;
}
template <class T, class U, enable_if_t<is_integral_v<U>, nullptr_t> = nullptr>
constexpr T Powmod(T a, U n, T mod) {
	assert(n >= 0);
	if (a > mod) a %= mod;
	T result = 1;
	while (n > 0) {
		if (n & 1) {
			result = result * a % mod;
			n--;
		} else {
			a = a * a % mod;
			n >>= 1;
		}
	}
	return result;
}
template <class T> bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> int sz(const T& v) {
	return v.size();
}
template <class T, class U> int lower_index(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> int upper_index(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T, class U = typename T::value_type> U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
namespace internal {
	template <class T, size_t N> auto make_vector(vector<int>& sizes, const T& init) {
		if constexpr (N == 1) {
			return vector(sizes[0], init);
		} else {
			int size = sizes[N - 1];
			sizes.pop_back();
			return vector(size, make_vector<T, N - 1>(sizes, init));
		}
	}
}  // namespace internal
template <class T, size_t N>
auto make_vector(const int (&sizes)[N], const T& init = T()) {
	vector s(rbegin(sizes), rend(sizes));
	return internal::make_vector<T, N>(s, init);
}
#line 9 "/home/yuruhiya/programming/library/template/template.cpp"
#if __has_include(<library/dump.hpp>)
#include <library/dump.hpp>
#define LOCAL
#else
#define dump(...) ((void)0)
#endif

template <class T> constexpr T oj_local(const T& oj, const T& local) {
#ifndef LOCAL
	return oj;
#else
	return local;
#endif
}
#line 5 "/home/yuruhiya/programming/library/Graph/GraphTemplate.cpp"
using namespace std;

using Weight = long long;
constexpr Weight INF = numeric_limits<Weight>::max();
struct Edge {
	int to;
	Weight cost;
	Edge() : to(-1), cost(-1) {}
	Edge(int _to, Weight _cost = 1) : to(_to), cost(_cost) {}
	friend bool operator<(const Edge& e1, const Edge& e2) {
		return e1.cost < e2.cost;
	}
	friend bool operator>(const Edge& e1, const Edge& e2) {
		return e1.cost > e2.cost;
	}
	friend ostream& operator<<(ostream& os, const Edge& e) {
		return os << "->" << e.to << '(' << e.cost << ')';
	}
};
using Graph = vector<vector<Edge>>;
struct Edge2 {
	int from, to;
	Weight cost;
	Edge2() : from(-1), to(-1), cost(0) {}
	Edge2(int _from, int _to, Weight _cost) : from(_from), to(_to), cost(_cost) {}
	friend bool operator<(const Edge2& e1, const Edge2& e2) {
		return e1.cost < e2.cost;
	}
	friend bool operator>(const Edge2& e1, const Edge2& e2) {
		return e1.cost > e2.cost;
	}
	friend ostream& operator<<(ostream& os, const Edge2& e) {
		return os << e.from << "->" << e.to << '(' << e.cost << ')';
	}
};
using Edges = vector<Edge2>;
using Matrix = vector<vector<Weight>>;
#line 5 "/home/yuruhiya/programming/library/Graph/Dijkstra.cpp"
using namespace std;

vector<Weight> Dijkstra(const Graph& graph, int s) {
	int V = graph.size();
	vector<Weight> dist(V, INF);
	dist[s] = 0;
	priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
	pq.emplace(s, 0);
	while (!pq.empty()) {
		Edge p = pq.top();
		pq.pop();
		int v = p.to;
		if (dist[v] < p.cost) continue;
		for (auto e : graph[v]) {
			if (dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				pq.emplace(e.to, dist[e.to]);
			}
		}
	}
	return dist;
}
Weight Dijkstra(const Graph& graph, int s, int t) {
	int V = graph.size();
	vector<Weight> dist(V, INF);
	dist[s] = 0;
	priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
	pq.emplace(s, 0);
	while (!pq.empty()) {
		Edge p = pq.top();
		pq.pop();
		int v = p.to;
		if (v == t) return dist[t];
		if (dist[v] < p.cost) continue;
		for (auto e : graph[v]) {
			if (dist[e.to] > dist[v] + e.cost) {
				dist[e.to] = dist[v] + e.cost;
				pq.emplace(e.to, dist[e.to]);
			}
		}
	}
	return dist[t];
}
#line 3 "a.cpp"

int main() {
	ini(n, x);
	auto [d, c] = in.multiple<int, int>(n);

	const int MAX_X = TEN(5) * 3;
	Graph g(MAX_X);
	rep(x, MAX_X) {
		rep(i, n) {
			if (x - d[i] >= 0) {
				g[x].emplace_back(x - d[i], c[i]);
			}
			if (x + d[i] < MAX_X) {
				g[x].emplace_back(x + d[i], c[i]);
			}
		}
	}
	ll ans = Dijkstra(g, 0, x);
	out(ans < inf_ll ? ans : -1);
}
accuracy: 1
time_ms: 170
memory_kb: 172452
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
int INF = 100000000;
int main(){
  int N, x;
  cin >> N >> x;
  vector<int> d(N), c(N);
  for (int i = 0; i < N; i++){
    cin >> d[i] >> c[i];
  }
  vector<int> cost(200001, INF);
  cost[0] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()){
    int c_cost = pq.top().first;
    int pos = pq.top().second;
    pq.pop();
    for (int i = 0; i < N; i++){
      if (pos + d[i] <= 200000){
        if (cost[pos + d[i]] > c_cost + c[i]){
          cost[pos + d[i]] = c_cost + c[i];
          pq.push(make_pair(cost[pos + d[i]], pos + d[i]));
        }
      }
    }
    for (int i = 0; i < N; i++){
      if (pos - d[i] >= 0){
        if (cost[pos - d[i]] > c_cost + c[i]){
          cost[pos - d[i]] = c_cost + c[i];
          pq.push(make_pair(cost[pos - d[i]], pos - d[i]));
        }
      }
    }
  }
  if (cost[x] == INF){
    cout << -1 << endl;
  } else {
    cout << cost[x] << endl;
  }
}
accuracy: 1
time_ms: 120
memory_kb: 5604
final_rank: 3

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

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

// constexpr i32 mod   = 998244353;
constexpr i32 mod   = 1e9 + 7;
constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(int x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solver() {
    int n, x;
    cin >> n >> x;

    vector< int > ds(n), cs(n);
    range(i, 0, n) {
        cin >> ds[i] >> cs[i];
    }

    vector< int > dist(101010 * 5, inf);
    int s = 101010 * 3;
    int g = 101010 * 3 + x;

    using pii = pair< int, int >;
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.emplace(0, s);
    dist[s] = 0;

    while (!pq.empty()) {
        pii p = pq.top();
        pq.pop();

        int v = p.second;
        if (dist[v] < p.first) continue;

        range(i, 0, n) {
            int u = v + ds[i];
            if (u < 0 || dist.size() <= u) continue;
            if (dist[u] > dist[v] + cs[i]) {
                dist[u] = dist[v] + cs[i];
                pq.emplace(dist[u], u);
            }

            u = v - ds[i];
            if (u < 0 || dist.size() <= u) continue;
            if (dist[u] > dist[v] + cs[i]) {
                dist[u] = dist[v] + cs[i];
                pq.emplace(dist[u], u);
            }
        }
    }


    if (dist[g] == inf) {
        cout << -1 << endl;
    } else {
        cout << dist[g] << endl;
    }
}

signed main(int argc, char *argv[]) {
    solver();
}
accuracy: 1
time_ms: 240
memory_kb: 8968
final_rank: 7

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 MAX 200005

struct Edge{
	Edge(int arg_diff,ll arg_cost){
		diff = arg_diff;
		cost = arg_cost;
	}

	int diff;
	ll cost;
};

struct Info{
	Info(int arg_x,ll arg_sum_cost){
		x = arg_x;
		sum_cost = arg_sum_cost;
	}
	bool operator<(const struct Info &arg) const{

		return sum_cost > arg.sum_cost; //総コストの昇順(PQ)
	}
	int x;
	ll sum_cost;
};

bool rangeCheck(int x){

	return x >= 0 && x < MAX;
}

int N;
int goal;
vector<Edge> edge;
ll min_cost[MAX];

int main(){

	scanf("%d %d",&N,&goal);

	int diff;
	ll cost;

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

		scanf("%d %lld",&diff,&cost);
		edge.push_back(Edge(diff,cost));
	}

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

		min_cost[i] = HUGE_NUM;
	}

	priority_queue<Info> Q;

	min_cost[0] = 0;
	Q.push(Info(0,0));

	while(!Q.empty()){

		if(Q.top().sum_cost > min_cost[Q.top().x]){

			Q.pop();

		}else if(Q.top().x == goal){

			printf("%lld\n",Q.top().sum_cost);
			return 0;

		}else{

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

				ll next_cost = Q.top().sum_cost+edge[i].cost;

				int left = Q.top().x-edge[i].diff;
				if(rangeCheck(left) == true && min_cost[left] > next_cost){

					min_cost[left] = next_cost;
					Q.push(Info(left,next_cost));
				}

				int right = Q.top().x+edge[i].diff;
				if(rangeCheck(right) == true && min_cost[right] > next_cost){

					min_cost[right] = next_cost;
					Q.push(Info(right,next_cost));
				}
			}

			Q.pop();
		}
	}

	printf("-1\n");

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8548
final_rank: 1

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
#define OFF 200000
typedef pair<int,int> P;
typedef long long ll;
ll d[20];
ll c[20];
ll dist[400010];
ll n,x;
void dijkstra() {
	priority_queue<P, vector<P>, greater<P> >que;
	fill(dist, dist + 400010, (ll)INF);
	dist[0+OFF] = 0;
	que.push(P(0, 0+OFF));
	while (!que.empty()) {
		P p = que.top();que.pop();
		ll v = p.second;
		if (dist[v] < p.first)continue;
		for (ll i = 0;i < n;i++) {
			int x=v+d[i];
            int y=v-d[i];
            if(x>400000)continue;
            if(y<0)continue;
			if (dist[x] > dist[v] + c[i]) {
				dist[x] = dist[v] + c[i];
				que.push(P(dist[x], x));
			}
            if (dist[y] > dist[v] + c[i]) {
				dist[y] = dist[v] + c[i];
				que.push(P(dist[y], y));
			}
		}
	}
}

int main(void){
    cin>>n>>x;
    for(int i=0;i<n;i++)cin>>d[i]>>c[i];
    dijkstra();
    if(dist[x+OFF]==INF)cout<<-1<<endl;
    else cout<<dist[x+OFF]<<endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 10024
final_rank: 4

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

using pli = std::pair<long long, int>;

int main()
{
	int N, x;
	scanf("%d%d", &N, &x);
	std::vector<int> d(N), c(N);
	for (int i{}; i < N; i++)
		scanf("%d%d", &d[i], &c[i]);

	int max_place{x + 2 * *std::max_element(d.begin(), d.end())};
	std::vector<long long> dist(max_place + 1, 1LL << 60);
	std::priority_queue<pli, std::vector<pli>, std::greater<pli>> dij;
	dist[0] = 0;
	dij.push({0, 0});
	while (!dij.empty())
	{
		auto now{dij.top()};
		dij.pop();
		if (now.first > dist[now.second]) continue;
		if (now.second == x) break;
		for (int i{}; i < N; i++)
		{
			int next[2]{now.second - d[i], now.second + d[i]};
			for (int j{}; j < 2; j++)
			{
				if (next[j] < 0 || max_place < next[j]) continue;
				if (dist[next[j]] <= now.first + c[i]) continue;
				dist[next[j]] = now.first + c[i];
				dij.push({dist[next[j]], next[j]});
			}
		}
	}
	if (dist[x] == (1LL << 60))
		puts("-1");
	else
		printf("%lld\n", dist[x]);

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 9284
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define fi first
#define se second
#define inf (int)1e8
using namespace std;

struct data {
  int p;
  int c;
};

bool operator<(const data &l, const data &r) {
  if(l.c != r.c) return l.c > r.c;
  return l.p > r.p;
}

int n, x;
vector<int> dp;
vector<pair<int, int>> v;
priority_queue<data> pq;

bool ch(data nextp) {
  return nextp.p >= 0 && nextp.p < dp.size();
}
int solve();

int main() {
  cin >> n >> x;
  v.resize(n);
  dp.assign(4 * 100000 + 100, inf);
  for(int i = 0; i < n; ++i) cin >> v[i].fi >> v[i].se;
  cout << solve() << endl;
  return 0;
}

int solve() {
  x += 100050;
  pq.push({100050, 0});
  dp[100050] = 0;
  while(!pq.empty()) {
    data now = pq.top();
    pq.pop();
    for(int i = 0; i < n; ++i) {
      data nextp = now;
      nextp.p += v[i].fi;
      nextp.c += v[i].se;
      if(ch(nextp) && dp[nextp.p] > nextp.c) {
        dp[nextp.p] = nextp.c;
        pq.push(nextp);
      }
      nextp.p -= v[i].fi * 2;
      if(ch(nextp) && dp[nextp.p] > nextp.c) {
        dp[nextp.p] = nextp.c;
        pq.push(nextp);
      }
    }
  }
  if(dp[x] == inf)
    return -1;
  else
    return dp[x];
}
accuracy: 1
time_ms: 230
memory_kb: 8544
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define fi first
#define se second
#define inf (int)1e8
using namespace std;

struct data {
  int p;
  int c;
};

bool operator<(const data &l, const data &r) {
  if(l.c != r.c) return l.c > r.c;
  return l.p > r.p;
}

int n, x;
vector<int> dp;
vector<pair<int, int>> v;
priority_queue<data> pq;

bool ch(data nextp) {
  return nextp.p >= 0 && nextp.p < dp.size();
}
int solve();

int main() {
  cin >> n >> x;
  v.resize(n);
  dp.assign(4 * 100000 + 100, inf);
  for(int i = 0; i < n; ++i) cin >> v[i].fi >> v[i].se;
  cout << solve() << endl;
  return 0;
}

int solve() {
  x += 100050;
  pq.push({100050, 0});
  dp[100000] = 0;
  while(!pq.empty()) {
    data now = pq.top();
    pq.pop();
    for(int i = 0; i < n; ++i) {
      data nextp = now;
      nextp.p += v[i].fi;
      nextp.c += v[i].se;
      if(ch(nextp) && dp[nextp.p] > nextp.c) {
        dp[nextp.p] = nextp.c;
        pq.push(nextp);
      }
      nextp.p -= v[i].fi * 2;
      if(ch(nextp) && dp[nextp.p] > nextp.c) {
        dp[nextp.p] = nextp.c;
        pq.push(nextp);
      }
    }
  }
  if(dp[x] == inf)
    return -1;
  else
    return dp[x];
}
accuracy: 0.4
time_ms: 10
memory_kb: 4420
final_rank: 18

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

typedef pair<int,int> P;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);(i)++)
#define rep(i,n) repl(i,0,n)
#define dbg(x) cout<<#x<<"="<<x<<endl

#define INF INT_MAX/3

struct state{
  int x,d;
};
bool operator<(const state& a,const state& b){
  return a.d > b.d;
}

int m=100010;
int d[210021];

int main(){

  rep(i,210001)d[i]=INF;

  int n,x;
  vector<P> ps;
  cin>>n>>x;
  x+=m;
  rep(i,n){
    int a,b; 
    cin>>a>>b;
    ps.push_back(P(a,b));
  }

  priority_queue<state> que;
  que.push((state){m,0});
  while(que.size()){
    state crt=que.top(); que.pop();
    if(d[crt.x]!=INF)continue;
    d[crt.x]=crt.d;
    // dbg(crt.x); dbg(crt.d);
    rep(i,n)rep(j,2){
      int nx=crt.x+(j==0?ps[i].first:-ps[i].first);
      // dbg(nx);
      if(nx<0||nx>210000)continue;
      que.push((state){nx,crt.d+ps[i].second});
    }
  }
  if(d[x]==INF)cout<<-1<<endl;
  else cout<<d[x]<<endl;

}
accuracy: 1
time_ms: 820
memory_kb: 36624
final_rank: 15

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

#define REP(i,s,n) for(int i=s; i<n; ++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
#define MAXI 100010

typedef long long ll;
typedef pair<int, int> ii;

int main(){

	int n, x;
	cin >> n >> x;

	vector<pair<ll, ll>> v(n);
	rep(i, n) cin >> v[i].second >> v[i].first;
	sort(v.begin(), v.end());

	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;
	rep(i, n) qu.push(make_pair(0, 0));

	vector<bool> used(MAXI*3+1, false);

	while(!qu.empty()){
		auto tmp = qu.top(); qu.pop();

		if(used[tmp.second]) continue;
		used[tmp.second] = true;

		if(tmp.second == x){
			cout << tmp.first << endl;
			return 0;
		}

		rep(i, n){
			ll cost = tmp.first + v[i].first;
			ll nx1 = tmp.second + v[i].second;
			if(0 <= nx1 && nx1 <= 3*MAXI && !used[nx1]) qu.push(make_pair(cost, nx1));
			ll nx2 = tmp.second - v[i].second;
			if(0 <= nx2 && nx2 <= 3*MAXI && !used[nx2]) qu.push(make_pair(cost, nx2));
		}

	}

	cout << -1 << endl;

	return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 35740
final_rank: 12

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

#define REP(i,s,n) for(int i=s; i<n; ++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
#define MAXI 100010

typedef long long ll;
typedef pair<int, int> ii;

int main(){

	int n, x;
	cin >> n >> x;

	vector<pair<ll, ll>> v(n);
	rep(i, n) cin >> v[i].second >> v[i].first;
	sort(v.begin(), v.end());

	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;
	rep(i, n) qu.push(v[i]);

	vector<bool> used(MAXI*4+1, false);

	while(!qu.empty()){
		auto tmp = qu.top(); qu.pop();

		if(used[tmp.second]) continue;
		used[tmp.second] = true;

		if(tmp.second == x){
			cout << tmp.first << endl;
			return 0;
		}

		rep(i, n){
			ll cost = tmp.first + v[i].first;
			ll nx1 = tmp.second + v[i].second;
			if(0 <= nx1 && nx1 <= 4*MAXI) qu.push(make_pair(cost, nx1));
			ll nx2 = tmp.second - v[i].second;
			if(0 <= nx2 && nx2 <= 4*MAXI) qu.push(make_pair(cost, nx2));
		}

	}

	cout << -1 << endl;

	return 0;
}
accuracy: 0.4
time_ms: 10
memory_kb: 3136
final_rank: 17

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

#define REP(i,s,n) for(int i=s; i<n; ++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
#define MAXI 100010

typedef long long ll;
typedef pair<int, int> ii;

int main(){

	int n, x;
	cin >> n >> x;


	vector<pair<ll, ll>> v(n);
	rep(i, n) cin >> v[i].second >> v[i].first;
	sort(v.begin(), v.end());

	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;
	rep(i, n) {
		qu.push(v[i]);
		qu.push(make_pair(v[i].first, -v[i].second));
	}

	vector<bool> used(MAXI*5+10, false);

	while(!qu.empty()){
		auto tmp = qu.top(); qu.pop();

		if(used[tmp.second+2.5*MAXI+1]) continue;
		used[tmp.second+2.5*MAXI+1] = true;

		if(tmp.second == x){
			cout << tmp.first << endl;
			return 0;
		}

		rep(i, n){
			ll cost = tmp.first + v[i].first;
			ll nx1 = tmp.second + v[i].second;
			if(-2.5*MAXI <= nx1 && nx1 <= 2.5*MAXI) qu.push(make_pair(cost, nx1));
			ll nx2 = tmp.second - v[i].second;
			if(-2.5*MAXI <= nx2 && nx2 <= 2.5*MAXI) qu.push(make_pair(cost, nx2));
		}

	}

	cout << -1 << endl;

	return 0;
}
accuracy: 0.4
time_ms: 20
memory_kb: 3148
final_rank: 19

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

#define REP(i,s,n) for(int i=s; i<n; ++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
#define MAXI 100010

typedef long long ll;
typedef pair<int, int> ii;

int main(){

	int n, x;
	cin >> n >> x;


	vector<pair<ll, ll>> v(n);
	rep(i, n) cin >> v[i].second >> v[i].first;
	sort(v.begin(), v.end());

	priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> qu;
	rep(i, n) qu.push(v[i]);

	vector<bool> used(MAXI*5+10, false);

	while(!qu.empty()){
		auto tmp = qu.top(); qu.pop();

		if(used[tmp.second+2.5*MAXI+1]) continue;
		used[tmp.second+2.5*MAXI+1] = true;

		if(tmp.second == x){
			cout << tmp.first << endl;
			return 0;
		}

		rep(i, n){
			ll cost = tmp.first + v[i].first;
			ll nx1 = tmp.second + v[i].second;
			if(-2.5*MAXI <= nx1 && nx1 <= 2.5*MAXI) qu.push(make_pair(cost, nx1));
			ll nx2 = tmp.second - v[i].second;
			if(-2.5*MAXI <= nx2 && nx2 <= 2.5*MAXI) qu.push(make_pair(cost, nx2));
		}

	}

	cout << -1 << endl;

	return 0;
}
accuracy: 0.4
time_ms: 20
memory_kb: 3176
final_rank: 20