#include #include #include using namespace std; using ll = long long; int main(){ ll n, a, b, c; cin >> n >> a >> b >> c; vector>> graph(n * 2 + 1); // グラフの構築 // 操作1 for(int i = 1; i < n * 2; i++){ graph[i].emplace_back(i + 1, a); } // 操作2 for(int i = 1; i < n * 2; i++){ ll nwb = b, next = i % n, up = (n <= i); while(nwb <= n * a){ graph[i].emplace_back(n * up + next, nwb); nwb *= b; next *= i; if(next >= n) up = 1; next %= n; } } // 操作3 ll next = 1, up = 0; for(int i = 1; i < n * 2; i++){ next *= i; if(next >= n) up = 1; next %= n; graph[i].emplace_back(up * n + next, c); } vector cost(n * 2 + 1, 1e18); cost[1] = 0; priority_queue, vector>, greater>> que; que.push({0, 1}); while(que.size()){ auto[c, p] = que.top(); que.pop(); if(c != cost[p]) continue; for(auto edge : graph[p]){ if(cost[edge.first] > cost[p] + edge.second){ cost[edge.first] = cost[p] + edge.second; que.push({cost[edge.first], edge.first}); } } } cout << min(cost[n], cost[n * 2]) << endl; }