#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e4 + 21, MOD = (int)1e9+7;
short n,m,b[N];
int ft[N];
vector<pair<int,int>> g[N * 2];
vector<pair<short,short>> x[N];
void add(int v,int u,int w) {
g[v].push_back({u,w});
}
bool sgn(int x) {
return (x < n);
}
int d[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
int p;
cin >> b[i] >> p;
int d = b[i] % p;
x[p].emplace_back(d,i);
}
int it = n + n;
for(int i = 1;i < n;i++){
sort(x[i].begin(),x[i].end());
for(int j = 0;j < (int)x[i].size();j++){
vector<int> f;
for(int k = j;k < (int)x[i].size() && x[i][k].first == x[i][j].first;k++){
j = k;
f.push_back(b[x[i][k].second]);
}
sort(f.begin(),f.end());
for(int t = 0;t < (int)f.size();t++){
int prev = (t ? f[t - 1] : 0);
for(int k = f[t],_=0;k >= prev;k -= i){
g[f[t]].push_back({k,_});
_++;
}
int nx = (t < (int)f.size() - 1 ? f[t + 1] : n - 1);
for(int k = f[t],_=0;k <= nx;k += i){
g[f[t]].push_back({k,_});
_++;
}
}
}
x[i].clear();
}
set<pair<int,int>> st;
for(int i = 0;i <= it;i++){
d[i] = 1e9;
}
st.insert({0,b[0]});
d[b[0]] = 0;
while(!st.empty()){
auto [c,v] = (*st.begin());
st.erase(st.begin());
for(auto [to,w]:g[v]){
if(d[to] > d[v] + w){
st.erase({d[to],to});
d[to] = d[v] + w;
st.insert({d[to],to});
}
}
}
if(d[b[1]] == 1e9){
d[b[1]] = -1;
}
cout << d[b[1]];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2584 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2584 KB |
Output is correct |
7 |
Correct |
1 ms |
2576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2592 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2412 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2588 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2588 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2584 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2556 KB |
Output is correct |
7 |
Correct |
1 ms |
2556 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2728 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2588 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2904 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2856 KB |
Output is correct |
29 |
Correct |
4 ms |
3348 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2840 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
7 ms |
3676 KB |
Output is correct |
34 |
Correct |
7 ms |
3872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2408 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2596 KB |
Output is correct |
15 |
Correct |
2 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
3 ms |
2588 KB |
Output is correct |
19 |
Correct |
1 ms |
2584 KB |
Output is correct |
20 |
Correct |
2 ms |
2592 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2648 KB |
Output is correct |
24 |
Correct |
3 ms |
2648 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
5 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2736 KB |
Output is correct |
31 |
Correct |
3 ms |
2808 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
6 ms |
3860 KB |
Output is correct |
34 |
Correct |
6 ms |
3956 KB |
Output is correct |
35 |
Correct |
8 ms |
4444 KB |
Output is correct |
36 |
Correct |
3 ms |
2924 KB |
Output is correct |
37 |
Correct |
8 ms |
5212 KB |
Output is correct |
38 |
Correct |
10 ms |
5432 KB |
Output is correct |
39 |
Correct |
10 ms |
5468 KB |
Output is correct |
40 |
Correct |
14 ms |
5560 KB |
Output is correct |
41 |
Correct |
10 ms |
5468 KB |
Output is correct |
42 |
Correct |
7 ms |
3744 KB |
Output is correct |
43 |
Correct |
6 ms |
3540 KB |
Output is correct |
44 |
Correct |
5 ms |
3620 KB |
Output is correct |
45 |
Correct |
16 ms |
7760 KB |
Output is correct |
46 |
Correct |
16 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
1 ms |
2588 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
3 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2648 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2920 KB |
Output is correct |
31 |
Correct |
3 ms |
2912 KB |
Output is correct |
32 |
Correct |
2 ms |
2916 KB |
Output is correct |
33 |
Correct |
7 ms |
3936 KB |
Output is correct |
34 |
Correct |
8 ms |
4184 KB |
Output is correct |
35 |
Correct |
7 ms |
4444 KB |
Output is correct |
36 |
Correct |
3 ms |
2916 KB |
Output is correct |
37 |
Correct |
8 ms |
5212 KB |
Output is correct |
38 |
Correct |
10 ms |
5468 KB |
Output is correct |
39 |
Correct |
11 ms |
5652 KB |
Output is correct |
40 |
Correct |
10 ms |
5468 KB |
Output is correct |
41 |
Correct |
10 ms |
5448 KB |
Output is correct |
42 |
Correct |
6 ms |
3544 KB |
Output is correct |
43 |
Correct |
5 ms |
3544 KB |
Output is correct |
44 |
Correct |
6 ms |
3800 KB |
Output is correct |
45 |
Correct |
14 ms |
7952 KB |
Output is correct |
46 |
Correct |
15 ms |
8024 KB |
Output is correct |
47 |
Correct |
29 ms |
12636 KB |
Output is correct |
48 |
Runtime error |
14 ms |
12188 KB |
Execution killed with signal 11 |
49 |
Halted |
0 ms |
0 KB |
- |