char const* fnames[]={"hand","lily","factory","casino","grimoire"}; long bspeeds[]={1,10,120,2000,25000}; long bprices[]={150,2000,30000,600000,10000000}; char acts[]="AABBBBBBCBBDDBDDDBDAEDDFDFFFBDBBFFGFFCHDBBHBBHFHDHHIHHFJHJHJFEDDJFDDDJBBJKJGHFFJJFJFHFHDDBIHHJHJFHFDBBHJJJCDBBBBAKJJJHHEFJDDDFDDHGFFFFDBBJHFDBIHHHJHFHFBJDBKJJJJDHFBJCBBBBDHFBJDHFEDDDDBJDHFABGFFFFJDHFBIHHHHJDHFBJDKJJJJHFBJDHFBJCBBBBDHFBJDHFEDDDDBJDHFBGFFFFJDHFBIHHHHJADHFBJDKJJJJHFBJDHFBJDHFCBBBBBJDHF"; char events[10010]; char buf[100]; int asale[10010]; int akoma[10010]; int abonus[10010]; long buy_price(long base,int num){ long price=base; for(;num--;){ price=(price*6+4)/5; } return price; } long rein_price(long base,int lvl){ long price=base; for(;lvl--;){ price*=10; } return price; } main(){ gets(buf); gets(events); // jikken close(0); { int koma=0; int fever=0; int nextsale=20000; for(int turn=10000;turn--;){ if(events[turn]=='S'){ nextsale=turn; } asale[turn]=nextsale; koma+=events[turn]=='F'?121:1; } for(int turn=0;turn<10000;++turn){ akoma[turn]=koma; koma-=fever?(--fever,7):1; if(events[turn]=='F'){ fever=20; } } } long cookie=0; int cl=0; int n[5]={0,0,0,0,0}; int l[5]={0,0,0,0,0}; int nsum=0; int nowsale=0; int nowfever=0; char* pa=acts; for(int turn=0;turn<10000;++turn){ // action if(turn+nsum>=10000){ for(int i=0;i<5;++i){ if(n[i]){ n[i]-=1; cookie+=buy_price(bprices[i],n[i])/4; dprintf(1,"sell %s\n",fnames[i]); break; } } }else{ int done=0; int a=*pa; if(a=='A'){ // enhclick long price1=rein_price(15,cl); long price2=price1-price1/10; long price=nowsale?price2:price1; long delta=1ll<=price && delta*(asale[turn]-turn)>=price-price2 && akoma[turn]*delta>price ){ cookie-=price; cl+=1; done=1; dprintf(1,"enhclick\n"); } }else if(a>='B'&&a<='K'){ int i=a-'B'>>1; if(a&1){ // reinforce long price1=rein_price(bprices[i]*10,l[i]); long price2=price1-price1/10; long price=nowsale?price2:price1; long delta=bspeeds[i]*n[i]<=price && delta*(asale[turn]-turn)>=price-price2 && akoma[turn]*delta>price ){ cookie-=price; l[i]+=1; done=1; dprintf(1,"reinforce %s\n",fnames[i]); } }else{ // buy long price1=buy_price(bprices[i],n[i]); long price2=price1-price1/10; long price=nowsale?price2:price1; long delta=bspeeds[i]<=price && delta*(asale[turn]-turn)>=price-price2 && akoma[turn]*delta>price-price1/4 ){ cookie-=price; n[i]+=1; nsum+=1; done=1; dprintf(1,"buy %s\n",fnames[i]); } } } if(done){ ++pa; }else{ // click cookie+=(1ll<