Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

|

General

# Author Problem Lang Verdict Time Memory Sent Judged
67657187 Practice:
pritishn
1096D - 25 GNU C++17 Accepted 62 ms 4164 KB 2019-12-26 17:21:33 2019-12-26 17:21:33

→ Source
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
using namespace std;
void Onigiri()
{
int n;cin>>n;
string s;cin>>s;
int a[n];rep(i,n)cin>>a[i];
int dp[n][4];
dp[0][0]=0;dp[0][1]=0;
dp[0][2]=0;dp[0][3]=0;
for(int i=0;i<n;i++)
{
if(i!=0)dp[i][0]=dp[i-1][0],dp[i][1]=dp[i-1][1],dp[i][2]=dp[i-1][2],dp[i][3]=dp[i-1][3];
if(s[i]=='h')
{
dp[i][0]+=a[i];
}
else if(s[i]=='a')
{
dp[i][1]=min(a[i]+dp[i][1],dp[i][0]);
}
else if(s[i]=='r')
{
dp[i][2]=min(a[i]+dp[i][2],dp[i][1]);
}
else if(s[i]=='d')
{
dp[i][3]=min(a[i]+dp[i][3],dp[i][2]);
}
}
cout<<min(min(dp[n-1][3],dp[n-1][2]),min(dp[n-1][1],dp[n-1][0]));

}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
while(t--)
{
Onigiri();
cout<<"\n";
}
return 0;

}

?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
?
?
?