Under construction

Still being built. Things may change

Friends Calling Plan

Time limit1sMemory limit128 MB

Summary
Given call minutes between up to 16 employees, pair them all up to minimize total billing cost using bitmask DP over perfect matchings.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation, Combinatorics
Solved
No attempts yet

Problem

Clarke runs a small company. Because of the nature of the work, every employee was given a mobile phone so they can talk with other employees, and Clarke pays all of their phone bills. He wants to cut those bills as much as possible.

The phone company offers a 'friends' service: two people who often call each other may form a 'friends' pair, which makes the calls between the two of them cheaper. Each person may belong to at most one friends pair, and the price of talking with anyone else stays the same.

The price of a one-minute call is FF for a friends pair and RR otherwise. In other words, a single MM-minute call between two employees costs F×MF \times M if the two are a friends pair, and R×MR \times M otherwise. The total bill is the sum of the cost of every call.

Given last month's list of calls (who talked with whom and for how long), pair all employees into N/2N/2 friends pairs so that the company's total phone bill is as small as possible. Compute the smallest possible total bill.

Input

The first line contains two integers FF and RR separated by a space (1≤F≤R≤1001 \le F \le R \le 100). FF is the price of a one-minute call for a friends pair, and RR is the price of a one-minute call for people who are not a friends pair.

The second line contains an even integer NN (2≤N≤162 \le N \le 16), the number of employees, numbered from 11 to NN.

The third line contains an integer CC (1≤C≤100001 \le C \le 10000), the number of calls.

Each of the next CC lines contains three integers AA, BB, and MM separated by spaces (1≤A≤N1 \le A \le N, 1≤B≤N1 \le B \le N, 1≤M≤1001 \le M \le 100), meaning that employees AA and BB had a call lasting MM minutes.

Output

Print, on a single line, the smallest possible total phone bill, i.e. the minimum total cost achievable by pairing all employees into N/2N/2 friends pairs in the best possible way.

Examples3

  1. Example 1

    Input
    1 2
    4
    4
    2 3 18
    2 4 26
    2 3 2
    1 4 12
    
    Expected output
    84
    
  2. Example 2

    Input
    1 2
    8
    5
    5 3 14
    5 6 66
    7 8 72
    5 7 99
    6 1 17
    
    Expected output
    398
    
  3. Example 3

    Input
    3 10
    6
    4
    1 3 50
    3 5 85
    4 1 87
    2 3 73
    
    Expected output
    1746