DreamTeam Selection

Time limit: 0.08s Memory limit: 64MB Input: Output:

William, like most respectable Italians, pursues his personal conflict of interest with supreme passion. He really loves being both an organizer of the Italian IOIT contests, and coaching his personal team for the same competition! His team (the RoseToPCoders) did not made it through this year’s competition, so it’s time to plan for a better performance next year.

William has already selected a shortlist consisting of NN students, numbered from 00 to N1N − 1, each of them with a best friend FiF_i. Remember that best-friendship is always symmetrical: if aa’s best friend is bb, then bb’s best friend is aa. William knows his buddies very well: for each of them, he measured how many points PiP_i the student is able to score in a typical competition if his best friend is not in the team, and how many points QiPiQ_i \leq P_i the student is able to score if his best friend is in the team (the presence of a friend is always distracting).

Help William choose the best team consisting of exactly KK contestants for next year’s competition!

Input data

The first line contains two integers NN, KK. Each of the following NN lines contains 33 integers FiF_i, PiP_i, QiQ_i.

Output data

You need to write a single line with an integer: the maximum number of points that a team of exactly KK contestants (among the NN given) can score in a typical competition, taking friendships into account.

Constraints and clarifications

  • 1KN100 0001 \leq K \leq N \leq 100 \ 000.
  • 0Fi<N0 \leq F_i < N for each i=0N1i = 0 \dots N − 1.
  • 0QiPi20 0000 \leq Q_i \leq P_i \leq 20 \ 000 for each i=0N1i = 0 \dots N − 1.
  • Best-friendship is symmetrical: Fi=jF_i = j if and only if Fj=iF_j = i for each i,j=0N1i, j = 0 \dots N − 1.
  • NN is even and no student is best friend of himself: FiiF_i \neq i for each i=0N1i = 0 \dots N − 1.
# Score Constraints
1 5 Examples
2 10 K=1K = 1
3 25 K3K \leq 3
4 20 N10N \leq 10
5 20 N100N \leq 100
6 20 No additional limitations.

Example 1

stdin

4 1
2 20 15
3 70 0
0 10 10
1 50 0

stdout

70

Explanation

In the first sample case, the single best student is number 11.

Example 2

stdin

6 3
2 40 30
4 90 70
0 75 10
5 20 0
1 80 80
3 50 50

stdout

225

Explanation

In the second sample case, the best team consists of students 11, 22 and 44 for a total of 7070 + 7575 + 8080 = 225225 points (since 11 and 44 are best friends). The best team avoiding friendships would instead be 11, 22 and 55 scoring 9090 + 7575 + 5050 = 215215 points.

Log in or sign up to be able to send submissions!