#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
const int inf = 1e9;
const int mod = 1e9+7;
const int maxn = 30000;
void solve() {
int n, m;
cin >> n >> m;
int a[m], b[m];
vt<int> it[n];
int dp[n];
for(int i = 0; i < n; i++)dp[i] = inf;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i];
it[a[i]].pb(i);
}
priority_queue<pii, vt<pii>, greater<pii>> pq;
pq.push({0, a[0]});
dp[a[0]] = 0;
while(!pq.empty()){
pii nw = pq.top();
pq.pop();
if(dp[nw.ss] != nw.ff)continue;
for(auto i : it[nw.ss]){
int x = b[i];
for(int j = nw.ss - x, cnt = 1; j >= 0; cnt++, j -= x){
if(dp[j] > nw.ff + cnt){
dp[j] = nw.ff + cnt;
pq.push({dp[j], j});
}
}
for(int j = nw.ss + x, cnt = 1; j < n; cnt++, j += x){
if(dp[j] > nw.ff + cnt){
dp[j] = nw.ff + cnt;
pq.push({dp[j], j});
}
}
}
}
if(dp[a[1]] == inf)cout << -1;
else cout << dp[a[1]];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
356 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
472 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
368 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
476 KB |
Output is correct |
24 |
Correct |
1 ms |
616 KB |
Output is correct |
25 |
Correct |
2 ms |
612 KB |
Output is correct |
26 |
Correct |
3 ms |
360 KB |
Output is correct |
27 |
Correct |
3 ms |
572 KB |
Output is correct |
28 |
Correct |
1 ms |
360 KB |
Output is correct |
29 |
Correct |
2 ms |
616 KB |
Output is correct |
30 |
Correct |
1 ms |
616 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
548 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
3 ms |
356 KB |
Output is correct |
27 |
Correct |
3 ms |
564 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
452 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
600 KB |
Output is correct |
34 |
Correct |
3 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
1024 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
1116 KB |
Output is correct |
39 |
Correct |
8 ms |
1152 KB |
Output is correct |
40 |
Correct |
6 ms |
1116 KB |
Output is correct |
41 |
Correct |
7 ms |
1116 KB |
Output is correct |
42 |
Correct |
77 ms |
1176 KB |
Output is correct |
43 |
Correct |
40 ms |
1180 KB |
Output is correct |
44 |
Correct |
44 ms |
1176 KB |
Output is correct |
45 |
Correct |
5 ms |
1372 KB |
Output is correct |
46 |
Correct |
5 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
560 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
740 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
5 ms |
1208 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
4 ms |
1112 KB |
Output is correct |
38 |
Correct |
7 ms |
1116 KB |
Output is correct |
39 |
Correct |
6 ms |
1236 KB |
Output is correct |
40 |
Correct |
6 ms |
1128 KB |
Output is correct |
41 |
Correct |
6 ms |
1152 KB |
Output is correct |
42 |
Correct |
42 ms |
1172 KB |
Output is correct |
43 |
Correct |
37 ms |
1116 KB |
Output is correct |
44 |
Correct |
61 ms |
1116 KB |
Output is correct |
45 |
Correct |
5 ms |
1372 KB |
Output is correct |
46 |
Correct |
6 ms |
1236 KB |
Output is correct |
47 |
Correct |
13 ms |
2008 KB |
Output is correct |
48 |
Correct |
4 ms |
1884 KB |
Output is correct |
49 |
Correct |
4 ms |
1884 KB |
Output is correct |
50 |
Correct |
4 ms |
1864 KB |
Output is correct |
51 |
Correct |
23 ms |
3388 KB |
Output is correct |
52 |
Correct |
19 ms |
3032 KB |
Output is correct |
53 |
Correct |
11 ms |
3032 KB |
Output is correct |
54 |
Correct |
2 ms |
1476 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
488 ms |
2932 KB |
Output is correct |
57 |
Correct |
44 ms |
3452 KB |
Output is correct |
58 |
Correct |
299 ms |
1920 KB |
Output is correct |
59 |
Correct |
602 ms |
1992 KB |
Output is correct |
60 |
Correct |
363 ms |
1964 KB |
Output is correct |
61 |
Correct |
484 ms |
2140 KB |
Output is correct |
62 |
Correct |
7 ms |
2652 KB |
Output is correct |
63 |
Correct |
30 ms |
3052 KB |
Output is correct |
64 |
Correct |
27 ms |
4048 KB |
Output is correct |
65 |
Correct |
29 ms |
4056 KB |
Output is correct |
66 |
Correct |
49 ms |
4044 KB |
Output is correct |
67 |
Correct |
49 ms |
4044 KB |
Output is correct |