Under construction

Still being built. Things may change

Balatro

Time limit12sMemory limit1024 MB

Summary
Pick a subsequence of add and multiply cards, using at most k multiply cards, to maximize the final score for each length up to n.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Greedy, Math
Solved
No attempts yet

Problem

Cards are arranged in a row. Each card has a value and is one of two types: an add card (a) or a multiply card (m).

The score is computed as follows. The initial score is 0. Process the cards from left to right.

  • If the card is an add card, increase the score by the card's value.
  • If the card is a multiply card, multiply the score by the card's value.

The final score is the score after all cards are processed.

For each subsequence size, find the maximum score you can get from a subsequence of that size. If the limit on multiply cards makes that size impossible, find the maximum score for the largest possible size below it. The chosen cards keep their original order, and you cannot rearrange them. A subsequence does not have to be contiguous.

Input

The first line contains two integers nn (2≤n≤2⋅1052≤n≤2 \cdot10^5) and kk (0≤k≤n0≤k≤n), where nn is the number of cards and kk is the maximum number of multiply cards you can use.

Each of the next nn lines contains a letter ss and an integer vv. ss is either a (add card) or m (multiply card), and vv is the card's value (2≤v≤1 0002≤v≤1\,000).

The product of all multiply card values is at most 10910^9.

Output

Output nn lines. The ii-th line contains the maximum score for a subsequence of length at most ii, for ii from 1 to nn.

Examples2

  1. Example 1

    Input
    4 3
    a 3
    m 2
    a 8
    m 3
    
    Expected output
    8
    24
    33
    42
    
  2. Example 2

    Input
    6 1
    a 2
    m 5
    a 2
    a 2
    m 3
    a 3
    
    Expected output
    3
    10
    13
    18
    21
    21