A. Grandma Laura and Applestime limit per test1 secondmemory limit per test256 megabytesinputstandard Inputoutputstandard Output
Grandma Laura came to the sell some apples. During the day she sold all the apples she had. But grandma was old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the MO ment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So all buyer took some integral positive number of apples, but maybe he didn ' t pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of a Apple is p (the number p is even).
Print The total money grandma should has at the end of the day to check if some buyers cheated her.
Input
The first line contains the integers n and p (1≤ n ≤ 40, 2≤ P ≤1000) -the number of the buyers and the cost of one apple. It is guaranteed, the number p is even.
The next n lines contains the description of buyers. Each buyer was described with the string half if he simply bought half of the apples and with the string halfplus I F Grandma also gave him a half of an apple as a gift.
It is guaranteed this grandma have at least one apple at the start of the day and she have no apples at the end of the day.
Output
Print the only integer a -the total money grandma should has the at the end of the day.
Note that the answer can is too large, so you should use 64-bit an integer type to store it. In C + + You can use the long-long integer type and in Java -can use long integer type.
Examplesinput
2 10
Half
Halfplus
Output
15
Input
3 10
Halfplus
Halfplus
Halfplus
Output
55
Note
The first sample at the start of the day the Grandma had and the apples. First she sold one Apple and then she sold a half of the second Apple and gave a half of the second Apple as a present to The second buyer.
Test instructions: Half is to buy half of the Apple, Halfplus is to buy half of the apple and send half, ask the last to get how much money;
Idea: All * *, from the last beginning to count to the first one;
AC Code:
#include <Bits/Stdc++.H>Using NamespaceStd;StringStr[1004];Long LongN,P;IntMain(){Cin>>N>>P; For(IntI=0;I<N;I++) {Cin>>Str[I]; } Long LongSum=0,Ans=0; For(IntI=N-1;I>=0;I--) { If(Str[I]=="Halfplus") {Sum+=1;Ans+=Sum*P;Sum*=2 } else { Ans+=sum*p Sum*=2; } } Cout<<ans/2<<endl return 0; /span>
Codeforces 632A A. Grandma Laura and Apples