Language: C++

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define Rep(i,s,n) for(int i=s;i<n;i++)
#define per(i,s,n) for(int i=n-1;i>=s;i--)
#define fi first
#define se second
#define all(c) c.begin(),c.end()
typedef long long int ll;
typedef pair<ll, ll> P;
typedef long double ld;
void Ans(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}

const ll INF=9999999999999999999999999;




int main(){
  int t;
  cin>>t;
  
  rep(i,t){
    ll n,a,b,c,d;
    cin>>n>>a>>b>>c>>d;
    ll ans=INF;
    ll ad,bd,cd;
    if(a%d==0) {
      ad=a/d;
      ad*=2;
    }
    else {
      ad=a/d+1;
      ad*=2;
      ad--;
    }
    
    if(b%d==0) {
      bd=b/d;
      bd*=3;
    }
    else{
      bd=b/d+1;
      bd*=3;
      bd--;
    }
    
    
    if(c%d==0) {
      cd=c/d;
      cd*=5;
    }
    else {
      cd=c/d+1;
      cd*=5;
      cd--;
    }
    
    
    ll u=INF;
    u=min(u,ad);
    u=min(u,bd);
    u=min(u,cd);
  }
    
    //cout<<u<<endl;
    
    queue<P> q;
    q.push(P(n,0));
    
    while(!(q.empty())){
      P p=q.front();q.pop();
      if(p.fi<=u){
        ans=min(ans,p.se+d*p.fi);
        continue;
      }
      if((p.fi/5*4/c)>(p.fi/3*2/b)){
        if(p.fi%5==1){
        q.push(P((p.fi-1)/5,p.se+c+d));
      }
      if(p.fi%5==2){
        q.push(P((p.fi-2)/5,p.se+c+d+d));
      }
      if(p.fi%5==3){
        q.push(P((p.fi+2)/5,p.se+c+d+d));
      }
      if(p.fi%5==4){
        q.push(P((p.fi+1)/5,p.se+c+d));
      }
      if(p.fi%5==0){
        q.push(P(p.fi/5,p.se+c));
      }
      continue;
      }
      
      
      if((p.fi/3*2/b)>(p.fi/2*1/a)){
        if(p.fi%3==0){
        q.push(P(p.fi/3,p.se+b));
      }
        if(p.fi%3==1){
        q.push(P((p.fi-1)/3,p.se+b+d));
      }
      if(p.fi%3==2){
        q.push(P((p.fi+1)/3,p.se+b+d));
      }
        
        continue;
      }
        
        
        
        
        
      if(p.fi%2==0){
        q.push(P(p.fi/2,p.se+a));
      }
      if(p.fi%2==1){
        q.push(P((p.fi-1)/2,p.se+a+d));
      }
      if(p.fi%3==0){
        q.push(P(p.fi/3,p.se+b));
      }
      if(p.fi%5==0){
        q.push(P(p.fi/5,p.se+c));
      }
      if(p.fi%3==1){
        q.push(P((p.fi-1)/3,p.se+b+d));
      }
      if(p.fi%3==2){
        q.push(P((p.fi+1)/3,p.se+b+d));
      }
      if(p.fi%5==1){
        q.push(P((p.fi-1)/5,p.se+c+d));
      }
      if(p.fi%5==2){
        q.push(P((p.fi-2)/5,p.se+c+d+d));
      }
      if(p.fi%5==3){
        q.push(P((p.fi+2)/5,p.se+c+d+d));
      }
      if(p.fi%5==4){
        q.push(P((p.fi+1)/5,p.se+c+d));
      }
    }
    
    
    
    cout<<ans<<endl;
    
}
      
    
    
    



Compiler output:
a.cc:14:14: warning: integer constant is too large for its type
   14 | const ll INF=9999999999999999999999999;
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:69:14: error: 'n' was not declared in this scope
   69 |     q.push(P(n,0));
      |              ^
a.cc:73:16: error: 'u' was not declared in this scope
   73 |       if(p.fi<=u){
      |                ^
a.cc:74:9: error: 'ans' was not declared in this scope; did you mean 'Ans'?
   74 |         ans=min(ans,p.se+d*p.fi);
      |         ^~~
      |         Ans
a.cc:74:26: error: 'd' was not declared in this scope
   74 |         ans=min(ans,p.se+d*p.fi);
      |                          ^
a.cc:77:20: error: 'c' was not declared in this scope
   77 |       if((p.fi/5*4/c)>(p.fi/3*2/b)){
      |                    ^
a.cc:77:33: error: 'b' was not declared in this scope
   77 |       if((p.fi/5*4/c)>(p.fi/3*2/b)){
      |                                 ^
a.cc:79:36: error: 'd' was not declared in this scope
   79 |         q.push(P((p.fi-1)/5,p.se+c+d));
      |                                    ^
a.cc:82:36: error: 'd' was not declared in this scope
   82 |         q.push(P((p.fi-2)/5,p.se+c+d+d));
      |                                    ^
a.cc:85:36: error: 'd' was not declared in this scope
   85 |         q.push(P((p.fi+2)/5,p.se+c+d+d));
      |                                    ^
a.cc:88:36: error: 'd' was not declared in this scope
   88 |         q.push(P((p.fi+1)/5,p.se+c+d));
      |                                    ^
a.cc:97:20: error: 'b' was not declared in this scope
   97 |       if((p.fi/3*2/b)>(p.fi/2*1/a)){
      |                    ^
a.cc:97:33: error: 'a' was not declared in this scope
   97 |       if((p.fi/3*2/b)>(p.fi/2*1/a)){
      |                                 ^
a.cc:102:36: error: 'd' was not declared in this scope
  102 |         q.push(P((p.fi-1)/3,p.se+b+d));
      |                                    ^
a.cc:105:36: error: 'd' was not declared in this scope
  105 |         q.push(P((p.fi+1)/3,p.se+b+d));
      |                                    ^
a.cc:116:30: error: 'a' was not declared in this scope
  116 |         q.push(P(p.fi/2,p.se+a));
      |                              ^
a.cc:119:34: error: 'a' was not declared in this scope
  119 |         q.push(P((p.fi-1)/2,p.se+a+d));
      |                                  ^
a.cc:119:36: error: 'd' was not declared in this scope
  119 |         q.push(P((p.fi-1)/2,p.se+a+d));
      |                                    ^
a.cc:122:30: error: 'b' was not declared in this scope
  122 |         q.push(P(p.fi/3,p.se+b));
      |                              ^
a.cc:125:30: error: 'c' was not declared in this scope
  125 |         q.push(P(p.fi/5,p.se+c));
      |                              ^
a.cc:128:34: error: 'b' was not declared in this scope
  128 |         q.push(P((p.fi-1)/3,p.se+b+d));
      |                                  ^
a.cc:128:36: error: 'd' was not declared in this scope
  128 |         q.push(P((p.fi-1)/3,p.se+b+d));
      |                                    ^
a.cc:131:34: error: 'b' was not declared in this scope
  131 |         q.push(P((p.fi+1)/3,p.se+b+d));
      |                                  ^
a.cc:131:36: error: 'd' was not declared in this scope
  131 |         q.push(P((p.fi+1)/3,p.se+b+d));
      |                                    ^
a.cc:134:34: error: 'c' was not declared in this scope
  134 |         q.push(P((p.fi-1)/5,p.se+c+d));
      |                                  ^
a.cc:134:36: error: 'd' was not declared in this scope
  134 |         q.push(P((p.fi-1)/5,p.se+c+d));
      |                                    ^
a.cc:137:34: error: 'c' was not declared in this scope
  137 |         q.push(P((p.fi-2)/5,p.se+c+d+d));
      |                                  ^
a.cc:137:36: error: 'd' was not declared in this scope
  137 |         q.push(P((p.fi-2)/5,p.se+c+d+d));
      |                                    ^
a.cc:140:34: error: 'c' was not declared in this scope
  140 |         q.push(P((p.fi+2)/5,p.se+c+d+d));
      |                                  ^
a.cc:140:36: error: 'd' was not declared in this scope
  140 |         q.push(P((p.fi+2)/5,p.se+c+d+d));
      |                                    ^
a.cc:143:34: error: 'c' was not declared in this scope
  143 |         q.push(P((p.fi+1)/5,p.se+c+d));
      |                                  ^
a.cc:143:36: error: 'd' was not declared in this scope
  143 |         q.push(P((p.fi+1)/5,p.se+c+d));
      |                                    ^
a.cc:149:11: error: 'ans' was not declared in this scope; did you mean 'Ans'?
  149 |     cout<<ans<<endl;
      |           ^~~
      |           Ans