Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de...

20
Escola de Inverno da Maratona SBC de Programa¸ ao 2019 E s c o l a d e I n v e r n o U F R G S 2 0 1 9 Caderno de Problemas 4 Realização Apoio

Transcript of Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de...

Page 1: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

Escola de Invernoda Maratona SBC de Programacao

2019

Escola de Inverno UFRGS 2019

Caderno de Problemas 4

Realização

Apoio

Page 2: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

2

Page 3: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

A – Apple Business6s per test, 512MB

Chitanda owns a farm with n apple trees on it, labeled by 1, 2, . . . , n, the i-th apple tree has ai apples.There are n − 1 bidirectional roads connecting them. For all i > 1, an apple tree number i is connectedby a road to the apple tree number ⌊ i

2⌋. So the map of the farm is also like a tree.

Apple business is booming. There are m requests, labeled by 1, 2, . . . ,m. For the i-th request, Chitandacan sell the customer at most ci apples, and the customer will pay wi dollars for each apple. Unfortunately,different customers have different preferences. Specifically, for the i-th request, the customer will give twointegers ui and vi, denoting he only accepts apples from trees on the shortest path from the ui-th appletree to the vi-th apple tree on the map(include ui and vi).

Assume the root of the map is the 1-th apple tree. It is amazing that ui is always vi’s ancestor or ui = vi.

The farm may not be able to sell ci apples to each customer. Please write a program to help Chitandasell apples that will maximize his profits.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there are two integers n,m(1 ≤ n,m ≤ 100000) in the first line, denoting the numberof apple trees and requests.

In the second line, there are n integers a1, a2, ..., an(1 ≤ ai ≤ 109), denoting the number of apples on eachapple tree.

For the next m lines, each line contains four integers ui, vi, ci, wi(1 ≤ ui, vi ≤ n, 1 ≤ ci ≤ 109, 1 ≤ wi ≤ 104),denoting each request. It is guaranteed that ui is vi’s ancestor or ui = vi.

It is guaranteed that∑

n ≤ 106 and∑

m ≤ 106.

OutputFor each test case, print a single line containing an integer, denoting the maximum profits.

Examplestandard input standard output

15 32 1 3 1 12 5 2 32 4 2 41 2 3 1

13

3

Page 4: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

B – Balanced Diet2s per test, 512MB

Taylor is wandering in a milk candy store. The store has m types of sweets and there are n sweets in thestore. The i-th sweet has the value of ai, and it is of type bi.

Taylor is planning to buy some sweets in the store, each sweet can be bought at most once. He will buyat least one sweet. Taylor knows that a balanced diet is important, the value of a sweet set is measuredas S

C , where S denotes the sum of ai and C denotes the maximum number of occurrences among all typesof sweets.

Assume Taylor selects pi sweets of type i, it is not welcomed if 1 ≤ pi < li. Note that pi can also be 0 andpi can be everything when li = 1.

Please write a program to help Taylor find the sweet set with maximum value.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there are two integers n,m(1 ≤ n,m ≤ 100000) in the first line, denoting the numberof sweets and types.

In the second line, there are m integers l1, l2, ..., lm(1 ≤ li ≤ n).

For the next n lines, each line contains two integers ai, bi(1 ≤ ai ≤ 108, 1 ≤ bi ≤ m), denoting each sweet.

It is guaranteed that∑

n ≤ 106 and∑

m ≤ 106, and there always exists a valid sweet set.

OutputFor each test case, print a single line of format u/v, denoting the maximum value u

v . Note that you shouldguarantee that gcd(u, v) = 1.

Examplestandard input standard output

22 127 12 13 21 22 15 23 2

9/25/1

4

Page 5: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

C – Line-line Intersection6s per test, 512MB

There are n lines l1, l2, . . . , ln on the 2D-plane.

Staring at these lines, Calabash is wondering how many pairs of (i, j) that 1 ≤ i < j ≤ n and li, lj shareat least one common point. Note that two overlapping lines also share common points.

Please write a program to solve Calabash’s problem.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there is one integer n(1 ≤ n ≤ 100000) in the first line, denoting the number of lines.

For the next n lines, each line contains four integers xai, yai, xbi, ybi(|xai|, |yai|, |xbi|, |ybi| ≤ 109). It meansli passes both (xai, yai) and (xbi, ybi). (xai, yai) will never be coincided with (xbi, ybi).

It is guaranteed that∑

n ≤ 106.

OutputFor each test case, print a single line containing an integer, denoting the answer.

Examplestandard input standard output

320 0 1 10 1 1 020 0 0 11 0 1 120 0 1 10 0 1 1

101

5

Page 6: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

D – Minimum Spanning Tree2s per test, 512MB

In the mathematical discipline of graph theory, the line graph of a simple undirected weighted graph G isanother simple undirected weighted graph L(G) that represents the adjacency between every two edgesin G.

Precisely speaking, for an undirected weighted graph G without loops or multiple edges, its line graphL(G) is a graph such that:

• Each vertex of L(G) represents an edge of G.

• Two vertices of L(G) are adjacent if and only if their corresponding edges share a common endpointin G, and the weight of such edge between this two vertices is the sum of their corresponding edges’weight.

A minimum spanning tree(MST) or minimum weight spanning tree is a subset of the edges of a connected,edge-weighted undirected graph that connects all the vertices together, without any cycles and with theminimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as smallas possible.

Given a tree G, please write a program to find the minimum spanning tree of L(G).

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there is one integer n(2 ≤ n ≤ 100000) in the first line, denoting the number of verticesof G.

For the next n − 1 lines, each line contains three integers u, v, w(1 ≤ u, v ≤ n, u = v, 1 ≤ w ≤ 109),denoting a bidirectional edge between vertex u and v with weight w.

It is guaranteed that∑

n ≤ 106.

OutputFor each test case, print a single line containing an integer, denoting the sum of all the edges’ weight ofMST (L(G)).

Examplestandard input standard output

241 2 12 3 23 4 341 2 11 3 11 4 1

84

6

Page 7: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

E – Mini-game Before Contest6s per test, 512MB

On the day before this contest, two CCPC teams are playing a game after dress rehearsal.

As we know, a CCPC team consists of 3 contestants, so there are 6 players in the game. The game isplayed on a directed graph. The graph has n vertices and m arcs. These vertices are labeled by 1, 2, . . . , n.

Initially, there is a token placed in one of the graph vertices. Players take turns moving the token alongone of the arcs that starts in the vertex the token is currently in. When there is no such arc, then thisplayer’s team loses the game.

Let’s mark two teams as A and B, then the order of 6 players can be displayed as a string ord with length6. Player from team ord1 moves first, then ord2, ord3, . . . , ord6, ord1 . . .

Each player will follow a strategy that leads his team to win, and if there is no possible to win, he willtry to make the game run infinitely. But some contestants announce they are “actors” before the game,each of them will follow a strategy that leads his team to lose, and if there is no possible to lose, he willtry to make the game run infinitely.

All the players know what the order is and who are actors. They also know preferences of other players.They will play optimally, but they are not allowed to discuss before or during the game.

For each initial position of the token, your task is to determine what kind of result the game is going tohave.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 100), denoting the number of test cases.

In each test case, there is two integers n(1 ≤ n ≤ 100000, 1 ≤ m ≤ 200000) in the first line, denoting thenumber of vertices and arcs.

For the next m lines, each line contains two integers ui, vi(1 ≤ ui, vi ≤ n), denoting an arc from vertex uito vertex vi. Note that ui can be the same with vi, but no arc will appear more than once.

The next line contains a string ord of length 6, denoting the order. There are exactly 3 of which are “A”and the others are “B”.

The next line contains six integers a1, a2, . . . , a6(0 ≤ ai ≤ 1), where ai denotes whether the playercorresponding to ordi is an actor. Specifically, ai = 1 means he is an actor while ai = 0 means not.

It is guaranteed that∑

n ≤ 2× 106 and∑

m ≤ 3× 106.

OutputFor each test case, print a single line containing n characters, where the i-th character should denote theresult of the game if the token is in vertex i initially. The result of the game is denoted by “A” if the teamA wins the game, “B” if the team B wins the game, and “D” if the game runs infinitely.

7

Page 8: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

Examplestandard input standard output

24 41 22 33 11 4AAABBB0000006 61 22 33 11 42 55 6AAABBB001000

DADBABBBBB

8

Page 9: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

F – Radar Scanner2s per test, 512MB

There are n rectangle radar scanners on the ground. The sides of them are all paralleled to the axes. Thei-th scanner’s bottom left corner is square (ai, bi) and its top right corner is square (ci, di). Each scannercovers some squares on the ground.

You can move these scanners for many times. In each step, you can choose a scanner and move it onesquare to the left, right, upward or downward.

Today, the radar system is facing a critical low-power problem. You need to move these scanners suchthat there exists a square covered by all scanners.

Your task is to minimize the number of move operations to achieve the goal.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there is one integer n(1 ≤ n ≤ 100000) in the first line, denoting the number of radarscanners.

For the next n lines, each line contains four integers ai, bi, ci, di(1 ≤ ai, bi, ci, di ≤ 109, ai ≤ ci, bi ≤ di),denoting each radar scanner.

It is guaranteed that∑

n ≤ 106.

OutputFor each test case, print a single line containing an integer, denoting the minimum number of steps.

Examplestandard input standard output

122 2 3 34 4 5 5

2

9

Page 10: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

G – Skyscraper4s per test, 512MB

At the main street of Byteland, there will be built n skyscrapers, standing sequentially one next to other.If look leftside right, sequence of their height will be a1, a2, . . . , an.

Initially the street is empty, every skyscraper’s height is 0. Hamster is the leader of the constructionteam. In each stage, Hamster can select a range [l, r], then the team will work on this range. Specifically,assume the height sequence is h1, h2, . . . , hn, then hl, hl+1, . . . , hr will increase by 1 during this stage.When hi = ai holds for all i ∈ [1, n], the project will be closed.

The plan may be changed for many times. There will be m events of 2 kinds below:

• 1 l r k (1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 105), for all x ∈ [l, r], change ax to ax + k.

• 2 l r (1 ≤ l ≤ r ≤ n), assume a1, a2, . . . , al−1, ar+1, ar+2, . . . , an = 0, ask for the minimum numberof required stages to close the project.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 1000), denoting the number of test cases.

In each test case, there are two integers n,m(1 ≤ n,m ≤ 100000) in the first line, denoting the numberof skyscrapers and events.

In the second line, there are n integers a1, a2, ..., an(1 ≤ ai ≤ 100000).

For the next m lines, each line describes an event.

It is guaranteed that∑

n ≤ 106 and∑

m ≤ 106.

OutputFor each query event, print a single line containing an integer, denoting the answer.

Examplestandard input standard output

15 41 3 1 4 52 1 51 3 4 22 2 42 1 5

766

10

Page 11: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

H – Time Limit2s per test, 512MB

In CCPC contests, you will get “Time Limit Exceeded” when your program tried to run during too muchtime. Setting suitable time limit for problems is vital to a contest.

Mr. Bread is preparing problems for a coming contest with his friends. For each problem, there will be a“Main Correct Solution” denotes the standard solution program written by the author. There will also beseveral “Correct Solutions” denote solution programs intended to pass.

Assume there are n programs in total, labeled by 1, 2, . . . , n. The 1-th program denotes the “Main CorrectSolution” while others are “Correct Solutions”. The i-th program runs in ai seconds.

According to the rules in Mr. Bread’s mind, the time limit x should meet all the rules below:

• The constraint can’t be too tight, which means x ≥ 3a1.

• All the “Correct Solutions” should pass, which means x ≥ ai + 1 for all i ∈ [2, n].

• x should be the smallest even integer meeting the rules described above.

Please write a program to find the time limit x.

InputThe first line of the input contains an integer T (1 ≤ T ≤ 10), denoting the number of test cases.

In each test case, there is one integer n(2 ≤ n ≤ 10) in the first line, denoting the number of programs.

In the second line, there are n integers a1, a2, ..., an(1 ≤ ai ≤ 10).

OutputFor each test case, print a single line containing an integer, denoting the value of x.

Examplestandard input standard output

221 321 4

46

11

Page 12: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

I – Strange Device4s per test, 512MB

Archaeologists have found a strange device that was probably created by some ancient civilization. Thedevice has a screen that displays two integers: x and y.

After exploring the device the scientists have made a conclusion that the device is kind of a clock.It measures time t passed from some moment in the past, but shows it in some weird way, probablyused by the creators of the device. If the time passed is an integer t, the two integers displayed are:x = ((t+

⌊tB

⌋) mod A), and y = (t mod B). Here ⌊x⌋ is the floor function — the greatest integer less or

equal to x.

The archaeologists have studied the device and found out that its screen wasn’t turned on all the time.Actually it was only working during n continuous periods of time, the i-th of them was from the momentli to the moment ri, inclusive. Now the scientists would like to calculate how many distinct pairs (x, y)were shown by the device when its screen was on.

Two pairs (x1, y1) and (x2, y2) are distinct if x1 = x2 or y1 = y2.

InputThe first line contains three integers n, A, and B (1 ≤ n ≤ 106; 1 ≤ A,B ≤ 1018).

Each of the following n lines contains two integers li and ri, the beginning and the end of the i-th segment[li, ri] when the device screen was turned on (0 ≤ li ≤ ri ≤ 1018; ri < li+1).

OutputOutput the number of distinct pairs (x, y) that were shown on the device screen when it was turned on.

Scoring

Let S =n∑

i=1(ri − li + 1) and L =

nmaxi=1

(ri − li + 1).

Subtask 1 (points: 10)S ≤ 106.

Subtask 2 (points: 5)n = 1.

Subtask 3 (points: 5)A ·B ≤ 106.

Subtask 4 (points: 5)B = 1.

Subtask 5 (points: 5)B ≤ 3.

Subtask 6 (points: 20)B ≤ 106.

12

Page 13: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

Subtask 7 (points: 20)L ≤ B.

Subtask 8 (points: 30)No additional constraint.

Examplesstandard input standard output

3 3 34 47 917 18

4

3 5 101 2050 6889 98

31

2 16 132 518 18

5

NoteIn the first test, the device screen shows the following integers.

t (x, y)

4 (2, 1)7 (0, 1)8 (1, 2)9 (0, 0)17 (1, 2)18 (0, 0)

So there are four distinct pairs (0, 0), (0, 1), (1, 2), (2, 1).

13

Page 14: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

14

Page 15: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

J – CompuTenis reloaded1s per test, 1536MB

You may remember from a previous edition of TAP about CompuTenis, that sportspecially adapted to a public without any mensurable physical qualities and whose rulesinvolve coding with your elbow glued to your ear. This time we will have another problemconcerning this exciting subject, in case you found the solution to that one too easy.

Once more, for the purposes of this problem the only thing you have to know aboutCompuTenis is that two players, which we will call A and B, compete in a match. Thematch is won by the player who �rst wins S sets, each set being composed of one or moregames. In each set the two players play as many games as is required for one of them towin in at least J games, with a di�erence of at least D games won in excess of hisopponent. The player satisfying both of these conditions is then the winner of thecorresponding set.

The Modern Club Association (MCA) has recently found a trove of records of pre-historicCompuTenis matches. Each record consists of a string composed of N characters 'A' or 'B',indicating which player won each of the N games the match had, in the order in whichthey occurred. Now the MCA wants to know, for each record, what the result of the matchwas.

InputThere are multiple test cases in the input �le. For each test case, the �rst line containsfour integers N, S, J and D. The value N represents the number of games in the record tobe analyzed (1 ≤ N ≤ 10 ). The value S indicates the number of sets a player is required towin in order to win the match (1 ≤ S ≤ 10). The value J is the minimum number of games itis necessary to win in order to win a set, whereas the value D indicates that a playershould win at least that number of games more than his opponent in order to win the set(1 ≤ D ≤ J ≤ 100). The second line contains a string composed of N characters 'A' or 'B'. Thei-th character of the string indicates which player won the i-th game played in the match.The input string will represent a valid record of a complete match.

OutputFor each test case, print a single line containing two integers representing the number ofsets won by player A and player B, respectively.

Example

Input:10 5 2 1AAAAAAAAAA21 3 3 2AABABBBABBBABABABBABB

Output:5 01 3

5

SPOJ.com - Problem TAP2015C https://www.spoj.com/problems/TAP2015C/en/

1 of 1 12/07/2019 10:08

15

Page 16: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

K – Hugo’s homework1s per test, 1536MB

Hugo goes to primary school, but he is convinced that not enough homework is beinggiven to him. Speci�cally, he was recently taught how to subtract two numbers, but eachday he is given a single subtraction to perform at home. Hugo knows that in order tomaster such a complex technique he should practice much more, so he has decided totake matters into his own hands and create his own homework.

It's not easy for Hugo to invent exercises about a subject he does not fully comprehend,so he has devised the following method to perform multiple subtractions. He starts byasking his mother for a number N, and then forms the number M containing the samedigits as N in increasing order from left to right, after which he �nally performs thesubtraction N-M. For example, if his mother chooses the number N = 321 then M = 123and the subtraction Hugo must perform is N - M = 321-123 = 198.

Hugo wouldn't want to bother his mother too often, so he will repeat this procedure usingthe result of the subtraction N-M in one step as the number N he starts with in the nextstep. This will end only when at some point he reaches the value N = 0, as this case isuseless to practice subtractions because he would have M = 0: Hugo already knowsperfectly well that if he has no candies he cannot eat any candies, and will thereforecontinue to not have or eat candies forever.

Now Hugo's mother would like to know, given a number N, how many subtractions Hugocan perform if she gives him that number to start his homework. In the previous example,in the second step Hugo would have N = 198 so that M = 189 and N - M = 198 - 189 = 9.Then in the third step N = 9, M = 9 and N - M = 0, so here the fun ends because in thefourth step he would have N = 0. Thus, starting with the number N = 321 Hugo willperform 3 subtractions.

InputThere are multiple test cases in the input �le. Each test case consists of one linecontaining an integer N, representing the number Hugo's mother will give him to start hishomework (1 ≤ N ≤ 10 ).

OutputFor each test case, print one line containing one integer representing the number ofsubtractions Hugo will perform if he starts his homework with number N.

Example

Input:32120960687301

Output:3291

9

SPOJ.com - Problem TAP2015H https://www.spoj.com/problems/TAP2015H/en/

1 of 1 12/07/2019 10:09

16

Page 17: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

L – Playing the ball2s per test, 256MB

Copy

Copy

Copy

A coder cannot sit and code all day. Sometimes it is a good idea to rise from the desk, have arest, have small talk with colleagues and even play. The coders of the F company have theirfavorite ball game.

Let's imagine the game on the plane with a cartesian coordinate system. The point (0, 0)contains the player who chooses an arbitrary direction and throws a ball in that direction. Theball hits the plane at distance d from the player's original position and continues flying in thesame direction. After the ball hits the plane for the first time, it flies on and hits the plane againat distance 2·d from the player's original position and so on (it continue flying in the chosendirection and hitting the plane after each d units). All coders in the F company are strong, sothe ball flies infinitely far away.

The plane has n circles painted on it. If a ball hits the plane and hits a circle that is painted onthe plane (including its border), then the player gets one point. The ball can hit multiple circlesat once and get one point for each of them (if the ball hits some circle x times during themove, the player also gets x points). Count the maximum number of points a player can get ifhe throws a ball in the arbitrary direction. Note that the direction may have real cooridinates.

InputThe first line contains two space-separated integers — n и d (1 ≤ n ≤ 2·10 ; 5 ≤ d ≤ 10).Next n lines contain the circles' description. The i-th line contains three space-separatedintegers x , y , r ( - 10000 ≤ x , y ≤ 10000; 1 ≤ r ≤ 50), where (x , y , r ) are thecoordinates of the center and the radius of the circle, correspondingly. The point (0, 0) is notinside or on the border of some circle.

OutputPrint a single integer — the maximum number of points you can get.

Examples

input

2 51 1 15 0 1

input

2 54 0 35 3 1

input

1 1020 0 10

4

i i i i i i i i

Copyoutput

1

Copyoutput

2

Copyoutput

3

Problem - 420E - Codeforces http://codeforces.com/problemset/problem/420/E

1 of 1 12/07/2019 10:06

17

Page 18: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

M – Gang Up4s per test, 512MB

Copy

Copy

The leader of some very secretive organization has decided to invite all other members to ameeting. All members of the organization live in the same town which can be represented as crossroads connected by two-directional streets. The meeting will be held in the leader'shouse near the crossroad . There are members of the organization invited to the meeting;-th of them lives near the crossroad .

All members of the organization receive the message about the meeting at the same momentand start moving to the location where the meeting is held. In the beginning of each minuteeach person is located at some crossroad. He or she can either wait a minute at thiscrossroad, or spend a minute to walk from the current crossroad along some street to anothercrossroad (obviously, it is possible to start walking along the street only if it begins or ends atthe current crossroad). In the beginning of the first minute each person is at the crossroadwhere he or she lives. As soon as a person reaches the crossroad number , he or sheimmediately comes to the leader's house and attends the meeting.

Obviously, the leader wants all other members of the organization to come up as early aspossible. But, since the organization is very secretive, the leader does not want to attractmuch attention. Let's denote the discontent of the leader as follows

initially the discontent is ;whenever a person reaches the crossroad number , the discontent of the leaderincreases by , where is some fixed constant, and is the number of minutes it tookthe person to reach the crossroad number ;whenever members of the organization walk along the same street at the samemoment in the same direction, is added to the discontent, where is some fixedconstant. This is not cumulative: for example, if two persons are walking along the samestreet in the same direction at the same moment, then is added to the discontent, not

.

Before sending a message about the meeting, the leader can tell each member of theorganization which path they should choose and where they should wait. Help the leader toestablish a plan for every member of the organization so they all reach the crossroad , andthe discontent is minimized.

InputThe first line of the input contains five integer numbers , , , and ( ,

, ) — the number of crossroads, the number of streets,the number of persons invited to the meeting and the constants affecting the discontent,respectively.

The second line contains numbers , , ..., ( ) — the crossroads wherethe members of the organization live.

Then lines follow, each denoting a bidirectional street. Each line contains two integers and ( , ) denoting a street connecting crossroads and .There may be multiple streets connecting the same pair of crossroads.

It is guaranteed that every crossroad can be reached from every other crossroad using thegiven streets.

OutputPrint one integer: the minimum discontent of the leader after everyone reaches crossroad .

Examples

input

3 2 4 2 33 3 3 31 22 3

input

3 3 4 2 33 2 2 3

𝑛𝑚1 𝑘

𝑖 𝑎𝑖

1

01

𝑐 ⋅ 𝑥 𝑐 𝑥1

𝑥𝑑𝑥2 𝑑

4𝑑5𝑑

1

𝑛 𝑚 𝑘 𝑐 𝑑 2 ≤ 𝑛 ≤ 50𝑛 − 1 ≤ 𝑚 ≤ 50 1 ≤ 𝑘, 𝑐, 𝑑 ≤ 50

𝑘 𝑎1 𝑎2 𝑎𝑘 2 ≤ ≤ 𝑛𝑎𝑖

𝑚 𝑥𝑖𝑦𝑖 1 ≤ , ≤ 𝑛𝑥𝑖 𝑦𝑖 ≠𝑥𝑖 𝑦𝑖 𝑥𝑖 𝑦𝑖

1

Copyoutput

52

Problem - 1187G - Codeforces http://codeforces.com/problemset/problem/1187/G

1 of 2 12/07/2019 10:02

18

Page 19: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns

Copy

Copy

Examples

input

3 2 4 2 33 3 3 31 22 3

input

3 3 4 2 33 2 2 31 22 32 3

NoteThe best course of action in the first test is the following:

the first person goes along the street to the crossroad , then goes along the street tothe crossroad and attends the meeting;the second person waits one minute on the crossroad , then goes along the street tothe crossroad , then goes along the street to the crossroad and attends the meeting;the third person waits two minutes on the crossroad , then goes along the street to thecrossroad , then goes along the street to the crossroad and attends the meeting;the fourth person waits three minutes on the crossroad , then goes along the street tothe crossroad , then goes along the street to the crossroad and attends the meeting.

Copyoutput

52

Copyoutput

38

2 2 11

3 22 1 1

3 22 1 1

3 22 1 1

Problem - 1187G - Codeforces http://codeforces.com/problemset/problem/1187/G

1 of 1 12/07/2019 10:03

19

Page 20: Caderno de Problemas 4 - UFRGS › maratona › EscolaInverno › 2019 › c4.pdf · Caderno de Problemas 4 Realização Apoio. 2. A { Apple Business 6s per test, 512MB Chitanda owns