PivotOJ

Pony-less Express

시간 제한: 8000ms메모리 제한: 2048MB출처: ICPC ECNA 2024-2025BOJ 32815

문제

Howdy, pardner! You've been put in charge of mail delivery in these here parts. Your headquarters is in Capital City, and Miss Penelope has asked us to tell all of the local farmsteads about her fashionable doin's. The roads hereabouts were built so that each farmstead connects to Capital City by only one set of roads, which may connect through multiple other farmsteads. Each road takes exactly one day to travel on a horse.

Each farmstead wants to receive news on their own specific timetable (so as not to distract the hired hands from their work), and gets peeved if the delivery misses the desired date, either early or late. Specifically, each farmstead ii has a perfect date DiD_i for receiving news. If the news arrive on day dd, the anger level at the farmstead is Ci(Did)2C_i(D_i - d)^2. Obviously, we want to minimize the total anger level over all the farmsteads, so we need to get on our horses and send this important information to each farmstead as close to their desired date as possible! The only problem is ... well ... we actually don't HAVE any horses. Instead, we can requisition exactly one horse from each location (farmstead or Capital City) each day, ride it for 11 day, and let it return to its starting location at the end of the day (don't worry, these horses know how to find their way back). The next day, another rider can take that horse and travel to a different location (if necessary). This process repeats in all of the farmsteads as well as Capital City.

Oh, and one more thing: we ain't payin' riders to sit idle at any farmstead, gittin' into who knows what kind of trouble. So if news arrives at farmstead ii on day dd and there are mm roads leading to mm other farmsteads that have not received any news yet, a horse will leave farmstead ii on each day d+1,d+2,d+md+1, d+2, \ldots d+m until every neighboring city has been visited, even if waiting extra days might lead to lower anger levels for some of the farmsteads.

For example, consider the layout of farmsteads shown in Figure K.1 (corresponding to Sample Input 11), where the Ci,DiC_i, D_i values are shown to the left of each farmstead. The optimal way to deliver the news is as follows:

  • Day 11: send a horse from Capital City to farmstead 33
  • Day 22: send a horse from Capital City to farmstead 11 and a horse from farmstead 33 to farmstead 77
  • Day 33: send a horse from Capital City to farmstead 22, a horse from farmstead 11 to farmstead 44 and a horse from farmstead 33 to farmstead 66
  • Day 44: send a horse from farmstead 11 to farmstead 55

The total anger level, adding up from farmstead 11 to 77, is 3(12)2+3(23)2+2(21)2+2(43)2+1(64)2+3(33)2+4(12)2=3+3+2+2+4+0+4=183(1-2)^2 + 3(2-3)^2 + 2(2-1)^2 + 2(4-3)^2 + 1(6-4)^2+3(3-3)^2+4(1-2)^2 = 3+3+2+2+4+0+4 = 18.

Figure K.1: Example layout.

Can you help us figure out just how angry people will be in total, so we know what we're in fer?

출력

The first line of input contains a positive integer nn (n200)(n \leq 200) indicating the number of farmsteads, numbered 11 to nn, with Capital City labeled 00. Following this are nn lines each containing three integers cc dd rr (1c20,1dn,0rn)(1 \leq c \leq 20, 1 \leq d \leq n, 0 \leq r \leq n), where the ithi^{th} of these lines specify the CiC_i and DiD_i values for the ithi^{th} farmstead and rr indicates that there is a road from farmstead ii to farmstead rr (or Capital City if r=0r=0). The roads are set up so that there is a unique path between any two farmsteads, and between each farmstead and Capital City.

예제

예제 1

입력
7
3 1 0
3 2 0
2 2 0
2 4 1
1 6 1
3 3 3
4 1 3
출력
18

예제 2

입력
3
5 2 2
4 1 0
6 3 1
출력
0
코드를 제출하려면 로그인하세요.