PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000113. Giao dịch thẻ bài (Card Trading)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giao dịch thẻ bài (Card Trading)

Card Trading

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

You own nn trading cards of tt types, indexed from 11 to tt. To form a collection, retain exactly two cards of each of exactly kk chosen types; sell all other cards and buy missing cards as needed. A card of type ii costs aia_i to buy and sells for bib_i. Maximize net earnings after assembling the collection.

Input

Line 1 contains n,t,kn,t,k. Line 2 contains the types of the nn owned cards. The next tt lines contain ai,bia_i,b_i for type ii.

Output

Print the maximum net earnings, possibly negative.

Subtasks

  • Subtask 1 (20 points): 1≤n,t≤121\le n,t\le 12; 1≤k≤t1\le k\le t; all lá thuộc loại [1,t][1,t]; 1≤ai,bi≤1091\le a_i,b_i\le10^9.
  • Subtask 2 (30 points): 1≤n,t≤25001\le n,t\le 2500; 1≤k≤t1\le k\le t; all lá thuộc loại [1,t][1,t]; 1≤ai,bi≤1091\le a_i,b_i\le10^9.
  • Subtask 3 (50 points): 1≤n,t≤1000001\le n,t\le 100000; 1≤k≤t1\le k\le t; all lá thuộc loại [1,t][1,t]; 1≤ai,bi≤1091\le a_i,b_i\le10^9.

Examples

Input

4 3 1
1 1 2 3
5 2
6 3
4 1

Output

4

Explanation

Selling everything earns 2⋅2+3+1=82\cdot2+3+1=8. Keeping a pair of type 11 costs 44 in forgone sale revenue, yielding 44. Completing type 22 costs 99 and type 33 costs 55, so type 11 is best.