Under construction

Still being built. Things may change

Waste Separation

Time limit1sMemory limit1024 MB

Summary
For each trash string, pick the cheaper option: its recycling bin if it is one recyclable type, otherwise general waste, and sum the minimum costs.
Level

Easy2 of 10

Topics
Implementation, String
Solved
No attempts yet

Problem

Jeongwoo has thrown away all of his trash as general waste so far. As a result, his disposal costs were higher than necessary, so he decided to cut costs by separating recyclables. Since he has never sorted waste before, Jiwon agreed to help him.

Jeongwoo has NN pieces of trash. Each piece is made of several components. The components are plastic (P)(P), can (C)(C), vinyl (V)(V), paper (S)(S), glass (G)(G), styrofoam (F)(F), and general waste (O)(O).

The sorting rules are as follows.

  • Items that can be recycled are plastic (P)(P), can (C)(C), vinyl (V)(V), paper (S)(S), glass (G)(G), and styrofoam (F)(F).
  • If a piece of trash consists of only one item XX, it can be put into the recycling bin for XX.
  • If a piece of trash consists of two or more different items, or it is not a recyclable item, it must be thrown away as general waste.
  • Any trash, including recyclables, can be thrown away as general waste.
  • The cost of putting trash into the recycling bin for item XX is CR=(size of the trash)×(cost per unit size of X)C_R = (\text{size of the trash}) \times (\text{cost per unit size of } X).
  • The cost of throwing trash away as general waste is CO=(size of the trash)×(cost per unit size of O)C_O = (\text{size of the trash}) \times (\text{cost per unit size of } O).
  • The size of a piece of trash equals the length of its string.

For example, suppose there are PPPPPPPP and PPGPPPGP, and the cost per unit size is 3 for plastic and 4 for general waste. PPPPPPPP has size 4 and can go into the plastic recycling bin, so the cost is 4×3=124 \times 3 = 12. PPGPPPGP contains two or more items, so it cannot go into a recycling bin. It must go to general waste, so the cost is 4×4=164 \times 4 = 16.

Jiwon wants to minimize Jeongwoo's disposal cost. Given NN pieces of trash, find the minimum total cost to dispose of all of them while following the sorting rules.

Input

The first line contains the number of pieces of trash NN. (1≤N≤10 000)(1 \le N \le 10\,000)

Each of the next NN lines contains a string describing one piece of trash. Each string has length at most 1 0001\,000 and consists only of the characters {P,C,V,S,G,F,O}\{P,C,V,S,G,F,O\}.

Line N+2N+2 contains the cost per unit size for plastic (P)(P), can (C)(C), vinyl (V)(V), paper (S)(S), glass (G)(G), and styrofoam (F)(F), in that order, separated by spaces. (1≤P,C,V,S,G,F≤10 000)(1 \le P, C, V, S, G, F \le 10\,000)

Line N+3N+3 contains the cost per unit size for general waste (O)(O). (1≤O≤10 000)(1 \le O \le 10\,000)

Output

Print the minimum total sorting cost for disposing of all the trash.

Hint

The total cost can exceed 231−12^{31}-1, so using a 64-bit integer type is recommended.

Examples2

  1. Example 1

    Input
    3
    OOOOO
    PPPPPPP
    PPPPFFFFF
    2 2 2 2 2 2
    3
    
    Expected output
    56
    
  2. Example 2

    Input
    10
    OOOO
    PPPPPP
    CC
    VVV
    GGG
    GGGG
    FFF
    GGG
    OOFFF
    OOGGVVFFSS
    3 8 5 2 9 4
    6
    
    Expected output
    231