#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9+7;
const int MX=4015;
ll n,m;
ll xx[MX],y[MX];
const int sq=2002;
ll dp[2205];
vector<ll> v[MX];
ll DP(ll x){
if(x<0||x>=n)R INF;
// cout<<x<<" "<<j<<endl;
if(x==xx[1])R 0;
ll &ret=dp[x];
if(ret!=-1)R ret;
ret=INF;
for(auto i : v[x]){
for(ll j=1;j<=n;j++){
ret=min(ret,DP(x-(j*i))+j);
ret=min(ret,DP(x+(j*i))+j);
}
}
R ret;
}
int main(){
MEM(dp,-1);
cin>>n>>m;
for(ll i=0;i<m;i++){
cin>>xx[i]>>y[i];
v[xx[i]].pb(y[i]);
}
if(DP(xx[0])==INF)cout<<-1;
else
cout<<DP(xx[0]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
608 KB |
Output is correct |
3 |
Correct |
1 ms |
648 KB |
Output is correct |
4 |
Correct |
1 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
720 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
720 KB |
Output is correct |
2 |
Correct |
1 ms |
720 KB |
Output is correct |
3 |
Correct |
2 ms |
720 KB |
Output is correct |
4 |
Correct |
2 ms |
720 KB |
Output is correct |
5 |
Correct |
1 ms |
720 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
720 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
3 |
Correct |
2 ms |
748 KB |
Output is correct |
4 |
Correct |
2 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
2 ms |
748 KB |
Output is correct |
11 |
Correct |
4 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
748 KB |
Output is correct |
13 |
Correct |
4 ms |
748 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
5 ms |
748 KB |
Output is correct |
16 |
Correct |
2 ms |
748 KB |
Output is correct |
17 |
Incorrect |
14 ms |
876 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
876 KB |
Output is correct |
2 |
Correct |
1 ms |
876 KB |
Output is correct |
3 |
Correct |
1 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
2 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
876 KB |
Output is correct |
8 |
Correct |
1 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
876 KB |
Output is correct |
11 |
Correct |
4 ms |
876 KB |
Output is correct |
12 |
Correct |
2 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
876 KB |
Output is correct |
14 |
Correct |
4 ms |
876 KB |
Output is correct |
15 |
Correct |
4 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Incorrect |
12 ms |
876 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
876 KB |
Output is correct |
2 |
Correct |
2 ms |
876 KB |
Output is correct |
3 |
Correct |
2 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
876 KB |
Output is correct |
5 |
Correct |
1 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
876 KB |
Output is correct |
7 |
Correct |
1 ms |
876 KB |
Output is correct |
8 |
Correct |
2 ms |
876 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
3 ms |
876 KB |
Output is correct |
11 |
Correct |
4 ms |
876 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
876 KB |
Output is correct |
14 |
Correct |
4 ms |
876 KB |
Output is correct |
15 |
Correct |
4 ms |
876 KB |
Output is correct |
16 |
Correct |
2 ms |
876 KB |
Output is correct |
17 |
Incorrect |
11 ms |
876 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |