This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define opa(x) cout << #x << " = " << x << ", ";
#define op(x) cout << #x << " = " << x << endl;
#define ops(x) cout << x;
#define entr cout << endl;
#define spac cout << ' ';
#define all(x) (x).begin(), (x).end()
#define STL(x) cout << #x << " : "; for(auto &qwe:x) cout << qwe << ' '; cout << endl;
#define arr(x, n) cout << #x << " : "; loop(qwe,0,n) cout << x[qwe] << ' '; cout << endl;
#define deb1 cout << "deb1" << endl;
#define deb2 cout << "deb2" << endl;
#define deb3 cout << "deb3" << endl;
#define deb4 cout << "deb4" << endl;
#define deb5 cout << "deb5" << endl;
using namespace std;
typedef long long ll;
const int mxn = 2510;
ll dp[mxn][mxn];
const ll INF = 1e18;
void solve(){
int n, A, B, C;
string s;
cin >> n >> s >> A >> B >> C;
loop(i,0,n+1){
loop(j,0,n+1){
dp[i][j] = INF;
}
}
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> pq;
pq.push({0, {0, 0}});
dp[0][0] = 0;
while(!pq.empty()){
int i = pq.top().ss.ff;
int j = pq.top().ss.ss;
ll dis = pq.top().ff;
pq.pop();
if(dis != dp[i][j]) continue;
if(dp[0][i] > dis + B){ // cut
pq.push({dis + B, {0, i}});
dp[0][i] = dis + B;
}
if(i + 1 <= n){
if(dp[i+1][j] > dis + A){ // add 'a'
pq.push({dis + A, {i+1, j}});
dp[i+1][j] = dis + A;
}
}
if(i + j <= n){
if(dp[i+j][j] > dis + C){ // add 'a'
pq.push({dis + C, {i+j, j}});
dp[i+j][j] = dis + C;
}
}
}
ll ans = INF;
loop(i,0,n+1){
ans = min(ans, dp[n][i]);
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
// freopen("test_input.txt", "r", stdin);
int t = 1;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |