luogu#P4643. [国家集训队] 阿狸和桃子的游戏

[国家集训队] 阿狸和桃子的游戏

Problem Description

Ali and Peach are playing a game on a weighted graph G=(V,E)G=(V,E). Let the weight of a vertex be w(v)w(v), and the weight of an edge be c(e)c(e). The rules are:

  1. Ali and Peach take turns coloring vertices in the graph. Ali colors a vertex red, and Peach colors a vertex pink. A vertex that has already been colored cannot be colored again, and in each turn, exactly one vertex must be colored.

  2. To ensure fairness, the number of vertices NN is even.

  3. After N2\frac{N}{2} rounds, each player obtains a set of vertices. For a vertex set SS, the score is

$$\sum_{v \in S}w(v) + \sum_{e=(u,v)\in E \land u,v\in S}c(e)$$

Since Ali lost Rock-Paper-Scissors to Peach, Peach colors first. Both players want their own score to exceed the other’s, and the larger the difference, the better. Assuming both players use optimal strategies, find the final value of Peach’s score minus Ali’s score.

Input Format

The first line contains two positive integers NN and MM, representing the number of vertices and edges of the graph GG, where NN is guaranteed to be even.

The next N+MN+M lines follow.

The first NN lines each contain one integer ww. The kk-th of these lines gives the weight of vertex kk.

The next MM lines each contain three integers a,b,ca,b,c separated by spaces, describing an edge between vertices aa and bb with weight cc.

Output Format

Output exactly one integer, which is Peach’s score minus Ali’s score.

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

Hint

Constraints and notes:

For 40%40\% of the testdata, 1N161 \le N \le 16.

For 100%100\% of the testdata, 1N100001 \le N \le 10000, 1M1000001 \le M \le 100000, 10000w,c10000-10000 \le w , c \le 10000.

Translated by ChatGPT 5