#include <bits/stdc++.h>
using namespace std;
#define oo 1000000000000000000ll
#define int long long
signed main() {
int n, m; cin >> n >> m;
vector<int> b(m), p(m);
for (int i = 0; i < m; ++i) cin >> b[i] >> p[i];
vector<vector<int>> v(n);
for (int i = 0; i < m; ++i) v[b[i]].push_back(i);
vector<vector<int>> ed(m, vector<int>(m, oo));
for (int i = 0; i < m; ++i){
int cnt = 0;
for (int j = b[i] + p[i]; j < n; j += p[i]) {
cnt++;
for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt);
}
for (int j = b[i] - p[i]; j >= 0; j -= p[i]){
cnt++;
for (auto to : v[j]) ed[i][to] = min(ed[i][to], cnt);
}
}
vector<int> d(m, oo); d[0] = 0;
vector<bool> visited(m);
for (int i = 0; i < m; ++i){
int mn = -1;
for (int j = 0; j < m; ++j){
if (!visited[j] && (mn == -1 || d[mn] > d[j])) mn = j;
}
visited[mn] = true;
for (int j = 0; j < m; ++j){
if (visited[j]) continue;
if (d[j] > d[mn] + ed[mn][j]) d[j] = d[mn] + ed[mn][j];
}
}
int ans = d[1];
if (ans == oo) ans = -1;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |