#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pp push
#define MOD 1000000007
#define INF 1e18
#define N 30005
#define M 5000000
typedef long long ll;
const ll c=174;
using namespace std;
ll b[N],p[N],path[N];
vector<ll>pos[N];
unordered_map<ll,bool>pp[N];
vector<pair<ll,ll> >adj[N];
bool vis[N],visited[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(path,-1,sizeof(path));
ll n,m;
cin >> n >> m;
for(ll i=1;i<=m;i++){
cin >> b[i] >> p[i];
if(pp[b[i]][p[i]]!=0){
vis[i]=1;
}
else{
pos[b[i]].pb(i);
pp[b[i]][p[i]]=1;
}
}
for(ll i=1;i<=m;i++){
if(vis[i]==1) continue;
for(ll j=b[i]+p[i];j<n;j+=p[i]){
if(pos[j].size()) adj[b[i]].pb({j,(j-b[i])/p[i]});
if(pp[j][p[i]]==1) break;
}
for(ll j=b[i]-p[i];j>=0;j-=p[i]){
if(pos[j].size()) adj[b[i]].pb({j,(b[i]-j)/p[i]});
if(pp[j][p[i]]==1) break;
}
}
priority_queue<pair<ll,ll> >pq;
pq.push({0,b[1]});
while(!pq.empty()){
ll cost=-pq.top().ff;
ll doge_id=pq.top().ss;
// cout << doge_id <<" " << cost << '\n';
path[doge_id]=cost;
pq.pop();
visited[doge_id]=1;
for(pair<int,int>i:adj[doge_id]){
pq.push({-(cost+i.ss),i.ff});
}
while(!pq.empty() && visited[pq.top().ss]==1) pq.pop();
}
cout << path[b[2]];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3692 KB |
Output is correct |
2 |
Correct |
3 ms |
3692 KB |
Output is correct |
3 |
Correct |
3 ms |
3712 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3692 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3692 KB |
Output is correct |
2 |
Correct |
3 ms |
3692 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3692 KB |
Output is correct |
6 |
Correct |
3 ms |
3712 KB |
Output is correct |
7 |
Correct |
3 ms |
3692 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
4076 KB |
Output is correct |
12 |
Correct |
4 ms |
3692 KB |
Output is correct |
13 |
Correct |
4 ms |
3692 KB |
Output is correct |
14 |
Correct |
5 ms |
4076 KB |
Output is correct |
15 |
Correct |
4 ms |
4076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3692 KB |
Output is correct |
2 |
Correct |
3 ms |
3692 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3692 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3692 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
4 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
4076 KB |
Output is correct |
12 |
Correct |
3 ms |
3692 KB |
Output is correct |
13 |
Correct |
3 ms |
3692 KB |
Output is correct |
14 |
Correct |
4 ms |
4076 KB |
Output is correct |
15 |
Correct |
4 ms |
4076 KB |
Output is correct |
16 |
Correct |
4 ms |
3968 KB |
Output is correct |
17 |
Correct |
8 ms |
4976 KB |
Output is correct |
18 |
Correct |
4 ms |
4076 KB |
Output is correct |
19 |
Correct |
4 ms |
3968 KB |
Output is correct |
20 |
Correct |
4 ms |
3948 KB |
Output is correct |
21 |
Correct |
3 ms |
3820 KB |
Output is correct |
22 |
Correct |
4 ms |
3948 KB |
Output is correct |
23 |
Correct |
5 ms |
4076 KB |
Output is correct |
24 |
Correct |
7 ms |
4588 KB |
Output is correct |
25 |
Correct |
6 ms |
4204 KB |
Output is correct |
26 |
Correct |
4 ms |
4076 KB |
Output is correct |
27 |
Correct |
4 ms |
3948 KB |
Output is correct |
28 |
Correct |
9 ms |
5228 KB |
Output is correct |
29 |
Correct |
14 ms |
6380 KB |
Output is correct |
30 |
Correct |
5 ms |
4332 KB |
Output is correct |
31 |
Correct |
8 ms |
5100 KB |
Output is correct |
32 |
Correct |
7 ms |
4864 KB |
Output is correct |
33 |
Correct |
32 ms |
9196 KB |
Output is correct |
34 |
Correct |
44 ms |
9068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3692 KB |
Output is correct |
2 |
Correct |
3 ms |
3692 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3692 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3692 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
4 ms |
3820 KB |
Output is correct |
11 |
Correct |
5 ms |
4076 KB |
Output is correct |
12 |
Correct |
4 ms |
3692 KB |
Output is correct |
13 |
Correct |
3 ms |
3692 KB |
Output is correct |
14 |
Correct |
4 ms |
4076 KB |
Output is correct |
15 |
Correct |
4 ms |
4076 KB |
Output is correct |
16 |
Correct |
4 ms |
3948 KB |
Output is correct |
17 |
Correct |
8 ms |
4976 KB |
Output is correct |
18 |
Correct |
4 ms |
4076 KB |
Output is correct |
19 |
Correct |
5 ms |
3948 KB |
Output is correct |
20 |
Correct |
4 ms |
3968 KB |
Output is correct |
21 |
Correct |
4 ms |
3820 KB |
Output is correct |
22 |
Correct |
4 ms |
3948 KB |
Output is correct |
23 |
Correct |
5 ms |
4076 KB |
Output is correct |
24 |
Correct |
7 ms |
4588 KB |
Output is correct |
25 |
Correct |
5 ms |
4204 KB |
Output is correct |
26 |
Correct |
5 ms |
4204 KB |
Output is correct |
27 |
Correct |
4 ms |
3948 KB |
Output is correct |
28 |
Correct |
10 ms |
5228 KB |
Output is correct |
29 |
Correct |
14 ms |
6380 KB |
Output is correct |
30 |
Correct |
5 ms |
4332 KB |
Output is correct |
31 |
Correct |
8 ms |
5100 KB |
Output is correct |
32 |
Correct |
7 ms |
4844 KB |
Output is correct |
33 |
Correct |
36 ms |
9196 KB |
Output is correct |
34 |
Correct |
45 ms |
9068 KB |
Output is correct |
35 |
Correct |
76 ms |
12900 KB |
Output is correct |
36 |
Correct |
13 ms |
5228 KB |
Output is correct |
37 |
Correct |
123 ms |
19676 KB |
Output is correct |
38 |
Correct |
99 ms |
19036 KB |
Output is correct |
39 |
Correct |
104 ms |
16992 KB |
Output is correct |
40 |
Correct |
103 ms |
17132 KB |
Output is correct |
41 |
Correct |
99 ms |
16992 KB |
Output is correct |
42 |
Correct |
9 ms |
4716 KB |
Output is correct |
43 |
Correct |
9 ms |
4588 KB |
Output is correct |
44 |
Correct |
10 ms |
4588 KB |
Output is correct |
45 |
Correct |
277 ms |
27512 KB |
Output is correct |
46 |
Correct |
270 ms |
27504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3692 KB |
Output is correct |
2 |
Correct |
3 ms |
3692 KB |
Output is correct |
3 |
Correct |
3 ms |
3692 KB |
Output is correct |
4 |
Correct |
3 ms |
3692 KB |
Output is correct |
5 |
Correct |
3 ms |
3692 KB |
Output is correct |
6 |
Correct |
3 ms |
3692 KB |
Output is correct |
7 |
Correct |
3 ms |
3692 KB |
Output is correct |
8 |
Correct |
3 ms |
3692 KB |
Output is correct |
9 |
Correct |
3 ms |
3692 KB |
Output is correct |
10 |
Correct |
3 ms |
3820 KB |
Output is correct |
11 |
Correct |
4 ms |
4076 KB |
Output is correct |
12 |
Correct |
3 ms |
3692 KB |
Output is correct |
13 |
Correct |
3 ms |
3692 KB |
Output is correct |
14 |
Correct |
4 ms |
4076 KB |
Output is correct |
15 |
Correct |
4 ms |
4076 KB |
Output is correct |
16 |
Correct |
4 ms |
3948 KB |
Output is correct |
17 |
Correct |
8 ms |
4976 KB |
Output is correct |
18 |
Correct |
4 ms |
4096 KB |
Output is correct |
19 |
Correct |
4 ms |
3948 KB |
Output is correct |
20 |
Correct |
4 ms |
3948 KB |
Output is correct |
21 |
Correct |
3 ms |
3820 KB |
Output is correct |
22 |
Correct |
4 ms |
3948 KB |
Output is correct |
23 |
Correct |
5 ms |
4076 KB |
Output is correct |
24 |
Correct |
8 ms |
4588 KB |
Output is correct |
25 |
Correct |
5 ms |
4204 KB |
Output is correct |
26 |
Correct |
4 ms |
4076 KB |
Output is correct |
27 |
Correct |
4 ms |
3948 KB |
Output is correct |
28 |
Correct |
12 ms |
5228 KB |
Output is correct |
29 |
Correct |
17 ms |
6380 KB |
Output is correct |
30 |
Correct |
6 ms |
4332 KB |
Output is correct |
31 |
Correct |
9 ms |
5100 KB |
Output is correct |
32 |
Correct |
7 ms |
4844 KB |
Output is correct |
33 |
Correct |
32 ms |
9068 KB |
Output is correct |
34 |
Correct |
33 ms |
9068 KB |
Output is correct |
35 |
Correct |
55 ms |
12900 KB |
Output is correct |
36 |
Correct |
14 ms |
5228 KB |
Output is correct |
37 |
Correct |
129 ms |
19548 KB |
Output is correct |
38 |
Correct |
108 ms |
19028 KB |
Output is correct |
39 |
Correct |
107 ms |
16992 KB |
Output is correct |
40 |
Correct |
123 ms |
17128 KB |
Output is correct |
41 |
Correct |
100 ms |
16992 KB |
Output is correct |
42 |
Correct |
9 ms |
4716 KB |
Output is correct |
43 |
Correct |
9 ms |
4588 KB |
Output is correct |
44 |
Correct |
9 ms |
4588 KB |
Output is correct |
45 |
Correct |
282 ms |
27464 KB |
Output is correct |
46 |
Correct |
281 ms |
27504 KB |
Output is correct |
47 |
Correct |
781 ms |
68048 KB |
Output is correct |
48 |
Correct |
136 ms |
17768 KB |
Output is correct |
49 |
Correct |
73 ms |
12672 KB |
Output is correct |
50 |
Correct |
64 ms |
11900 KB |
Output is correct |
51 |
Correct |
249 ms |
25220 KB |
Output is correct |
52 |
Correct |
285 ms |
29020 KB |
Output is correct |
53 |
Correct |
45 ms |
11372 KB |
Output is correct |
54 |
Correct |
8 ms |
5484 KB |
Output is correct |
55 |
Correct |
11 ms |
6380 KB |
Output is correct |
56 |
Correct |
41 ms |
8556 KB |
Output is correct |
57 |
Correct |
284 ms |
25292 KB |
Output is correct |
58 |
Correct |
18 ms |
7404 KB |
Output is correct |
59 |
Correct |
26 ms |
8684 KB |
Output is correct |
60 |
Correct |
41 ms |
9836 KB |
Output is correct |
61 |
Correct |
34 ms |
9452 KB |
Output is correct |
62 |
Correct |
378 ms |
37500 KB |
Output is correct |
63 |
Execution timed out |
1086 ms |
76136 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |