#include <bits/stdc++.h>
#define hash _hash_
#define left _left_
#define y1 _y1_
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll oo = 1e9;
/*----------- I alone decide my fate! ----------*/
/* Chiec den ong sao, sao 5 canh tuoi mau */
int N, M, a[30009], p[30009], st, dist[30009];
void Dijkstra() {
priority_queue <pair <int, int>, vector <pair <int ,int> >, greater <pair <int, int> > > pq;
pq.push({0, st});
while (!pq.empty()) {
pair <int, int> top = pq.top();
pq.pop();
int u = top.second, cost = top.first;
int jump = p[u];
if (u == a[2]) {
cout << cost; return;
}
if (dist[u] < cost) continue;
for (int v = u % p[u]; v <= N; v += p[u]) {
if (p[v]) {
if (dist[v] < dist[u] + abs(u - v)/jump) {
dist[v] = dist[u] + abs(u - v)/jump;
pq.push({dist[v], v});
}
}
}
}
cout << -1;
}
void solve() {
cin >> N >> M;
for (int i = 1; i <= M; i ++) {
cin >> a[i];
a[i] ++;
cin >> p[a[i]];
if (i == 1) st = a[i];
}
Dijkstra();
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
solve();
return 0;
}
/*
How can you see into my eyes, like open doors?
Leading you down into my core, where I've become so numb
Without a soul, my spirit's sleeping somewhere cold
Until you find it here and bring it back home!
Wake me up! Wake me up inside
Cant wake up? Wake me up inside
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |