// OBčh2009 Day2 B : Magic Slayer #include #include #include #include using namespace std; int main(){ int N, M; int HP[100]; int costS[100001], costA[100001]; while(cin >> N, N){ int maxHP = 0; for(int i=0;i> HP[i]; maxHP = max(maxHP, HP[i]); } cin >> M; string target; int MP, damage; vector< pair > single; vector< pair > all; for(int i=0;i> target >> MP >> target >> damage; if(damage==0) continue; if(target=="Single") single.push_back(make_pair(MP,damage)); else all.push_back(make_pair(MP,damage)); } memset(costS,-1,sizeof(costS)); costS[0] = 0; memset(costA,-1,sizeof(costA)); costA[0] = 0; for(int i=1;i<=maxHP;i++){ for(int j=0;jcostS[prev]+single[j].first) costS[i] = costS[prev]+single[j].first; } for(int j=0;jcostA[prev]+all[j].first) costA[i] = costA[prev]+all[j].first; } } int ans = -1; for(int i=0;i<=maxHP;i++){ if(costA[i]==-1) continue; if(costS[maxHP-i]==-1) continue; int tmp = costA[i]; for(int j=0;jtmp) ans = tmp; } cout << ans << endl; } }