1. 程式人生 > >codeforces-632A-Grandma Laura and Apples

codeforces-632A-Grandma Laura and Apples

A. Grandma Laura and Apples

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is 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 moment 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 each 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 an apple is p (the number p

 is even).

Print the total money grandma should have at the end of the day to check if some buyers cheated her.

Input

The first line contains two integers n and p (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.

The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.

It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.

Output

Print the only integer a — the total money grandma should have at the end of the day.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Examples

input

Copy

2 10
half
halfplus

output

Copy

15

input

Copy

3 10
halfplus
halfplus
halfplus

output

Copy

55

Note

In the first sample at the start of the day the grandma had two 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.

 

 

題意 
有n個顧客買蘋果,每個蘋果p元 
half就是這個顧客買了一半的蘋果 
halfplus就是這個顧客買了一半蘋果,最後還送了他半個蘋果 
最後恰好賣完所有蘋果 
問你賺了多少錢

題解: 
倒著推。 
當成模擬題做。

題目思路:倒推先求出蘋果的初始數量。首先我們可以確定,最後一個人購買一定是隻剩下1個蘋果了。往前推,如果前面一個人是half說明前面為偶數,如果是halfplus則為奇數

#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;

typedef long long ll;
long long ret[50];
int main(){
    int n,p;
    cin >> n >> p;
    long long ans = 1,tmp = 0;
    for(int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        if (s == "half") ret[i] = 1;
        else ret[i] = 2,tmp++;
    } 
    for (int i = n - 2; i>= 0; i--)
    {
        if (ret[i] == 1) ans = ans * 2;
        else ans = ans * 2 + 1;
    }
    ans = ans * p - tmp * (p / 2);
    cout << ans << endl;
    return 0;
}