LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Tasting Menu

MediumProblem #71
Time LimitMemoryInputOutput
2 s128 MBstdinstdout

A bonus depends on which dish came immediately before, so the set of dishes eaten is not enough on its own.

A restaurant offers a menu of n dishes, and you have decided to order exactly m of them, all different. Dish i on its own gives you ai​ units of enjoyment.

Some dishes taste better in a particular order, though. The chef has written down k pairings: a pairing x,y,c means that if you eat dish y immediately after dish x, with nothing in between, you gain another c units on top. A pairing only counts in the direction it is written.

You may eat your m dishes in any order you like. Find the largest total enjoyment you can reach.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains three integers n, m and k - the number of dishes on the menu, the number you will order, and the number of pairings.
The second line contains n integers a1​,a2​,…,an​ - the enjoyment each dish gives on its own.
Each of the next k lines contains three integers x, y and c - eating dish y immediately after dish x gives c extra enjoyment.

Dishes are numbered 1 to n. No pairing (x,y) is listed twice.

Output

For every testcase print a single line with the largest total enjoyment.

Example

Input
2
2 2 1
1 1
2 1 1
4 3 2
1 2 3 4
2 1 5
3 4 2
Output
3
12

In the first testcase eat dish 2 and then dish 1: one unit from each dish, plus one more for the pairing. In the second, ordering dishes 4,2,1 gives 4+2+1=7 from the dishes themselves, and the pairing 2→1 adds 5 - the order 2,1,4 scores the same 12.

Constraints

1≤t≤5
1≤m≤n≤18
0≤k≤n⋅(n−1)
0≤ai​≤109
1≤x,y≤n with x=y, and 0≤c≤109


This problem is based on Kefa and Dishes, problem 580D from Codeforces Round 321, by Mike Mirzayanov and the Codeforces team. The statement here is our own.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit