codeforces 632aGrandma Laura and Apples(背包)

A - Grandma Laura and Apples
Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 632A

Description

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 isp (the numberp 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 andp (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the numberp is even.

The next n lines contains the description of buyers. Each buyer is described with the stringhalf if he simply bought half of the apples and with the stringhalfplus 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 thelong long integer type and inJava you can use long integer type.

Sample Input

Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55


  小比赛碰到了这道题目,应该是很水的了,然而我却始终没有看懂此题目的意思,所以潜意识的认为这是一个很难的背包问题,然而事实让我吃了一鲸,受不了了。

  看懂了题意才是最关键的啊!!!

本题就是讲如果当时的苹果是偶数的话,卖一半,是奇数的话,则卖一半再加上一个苹果的一半(因为奇数的一半是.5),逆推就可以了。

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
    int n,p;
    string str[45];
    cin>>n>>p;
    long long sum=0,change=0;
    for(int i=n-1;i>=0;i--)
        cin>>str[i];
        for(int i=0;i<n;i++)
        {
            if(str[i]=="half")
            {
                sum+=change*p;
                change*=2;
            }
            else
            {
                sum+=change*p+1*p/2;
                change=change*2+1;
            }
        }
        cout <<sum<<endl;
    return 0;
}



你可能感兴趣的:(背包,杭电)