Problem A. Appeal To The Audience: Input
Problem A. Appeal To The Audience: Input
You are the director of the upcoming Bordfite Arena Progaming Competition. You have invited a bunch
of players and are now setting up the matches for the knockout tournament that will determine the winner.
As you may know, Bordfite Arena is a game that heavily rewards skill and very little luck is involved.
This implies that whenever any number of players play a game of Bordfite Arena, the most skilled player
will always win! Hence the winner of the tournament is already known, and you are a bit worried about
this. How will you appease the audience?
You embark on a short quest to find out what the audience finds interesting. No surprises there: people find
it most interesting when they see skillful players compete. Whenever a match is played, the happiness
the audience gets from a match is the sum of the skill levels of the players. The total happiness the
audience gets from the tournament is the sum of the happiness obtained during all matches. This is very
useful information, because of course you want the audience to be as happy as possible at the end of the
tournament.
Moreover, you invested some time to ask people what kind of knockout format they like. It turns out
that instead of the plain old binary tree for the knockout schedule, they prefer a specific weird-looking
rooted tree, and so you decide to use that. This means the final step for you to complete is to divide the
players over the leaves of the given tree so that over the entire tournament, the happiness of the audience
is maximized.
Input
• The first line contains integers 3 ≤ n ≤ 105 and 1 ≤ k ≤ n − 1, the number of nodes of the tree and
the number of players. The nodes are labelled 0 through n − 1, and 0 is the root of the tree.
• The second line contains k integers 0 ≤ a1 , . . . , ak ≤ 109 , denoting the skill values of the players.
• Then follow n − 1 lines, the ith of which (1 ≤ i ≤ n − 1) contains the parent 0 ≤ pi < i of node i.
It is guaranteed that the tree has exactly k leaves and that there are no nodes with exactly one child.
Output
Output the maximal possible happiness the audience can obtain from this tournament.
Example
Input Output
5 3 17
5 4 3
0
0
1
1
11 7 454
30 5 15 1 3 100 50
0
0
1
0
2
5
2
5
5
1
Call a set of positive integers triangular if it has size at least three and, for all triples of distinct integers
from the set, a triangle with those three integers as side lengths can be constructed.
Given a set of positive integers, compute the number of its triangular subsets.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 50), which is the number of integers in the set.
Each of the the next n lines contains a single integer x (1 ≤ x ≤ 109 ). These are the elements of the set.
They are guaranteed to be distinct.
Output
Output a single integer, which is the number of triangular subsets of the given set
Example
Input Output
5 2
3
1
5
9
10
10 58
27
26
17
10
2
14
1
12
23
39
A group of kangaroos live in houses on the number line. They all want to watch the Kangaroo Bowl!
Because not all of the kangaroos can fit a single house, they will designate two kangaroos to each host a
party at their house. All other kangaroos will choose to go to the house that is closest to them, picking
arbitrarily if they are the same distance from both.
A kangaroo expends (a−b)2 units of energy to travel from location a to location b. Compute the minimum
total units of energy expended if the two party house locations are chosen optimally.
Input
The first line of input contains a single integer n (2 ≤ n ≤ 50), which is the number of kangaroos.
Each of the next n lines contains a single integer x (−1, 000 ≤ x ≤ 1, 000), which is the location on the
number line of the house of one of the kangaroos. Each location will be distinct.
Output
Output, on a single line, the minimum total units of energy expended by all the kangaroos, given that
the party house locations are chosen optimally.
Example
Input Output
5 19
0
3
-3
10
11
48/50 10/49
0/50
50/100
Figure D.1: Illustration of Example Input 1. In this case Freyr only has to make 2 units of rain at the
left-most dam in order to make it break and send 50 units of water downstream, which then ultimately
results in 100 units of rain reaching the war camp, well exceeding the 75 units of water needed to flood
the camp.
Input
The first line of input consists of two integers n and w (1 ≤ n ≤ 2 · 105 , 1 ≤ w ≤ 109 ), the number of
dams and the amount of water needed to wash away the war camp, respectively. Then follow n lines,
describing the n dams. The dams are numbered from 1 to n.
The ith line contains three integers di , ci and ui (0 ≤ di < i, 1 ≤ ci ≤ 109 , 0 ≤ ui < ci ), where di is the
number of the dam immediately downstream of dam i (or 0 if the war camp is immediately downstream
of dam i), ci is the maximum capacity of dam i, and ui is the current amount of water in dam i.
Output
Output the minimum amount of rain Freyr needs to make at one location, which will result in at least w
water reaching the war camp.
Example
Input Output
4 75 2
0 100 50
1 49 10
1 50 0
3 50 48
4 13 10
0 12 1
1 6 1
2 4 1
3 10 0
4 1 1
0 100 50
1 49 10
1 50 0
3 50 48
N
P
x
- P
Figure E.1: Illustration of Example Input 1. Circles indicate components and the blue triangle indicates
where helium will spread when the first component is tested. This test being positive means that at least
one of the the three components covered by the triangle is leaking. The correct answer in this case is 1
since the measurement results can all be explained with only the rightmost component leaking.
Input
The first line of input contains an integer n (1 ≤ n ≤ 2 · 105 ), the number of components involved. The
following n lines each contain two integers x and y and a character c (−108 ≤ x, y ≤ 108 , c ∈ {-, P, N}),
where (x, y) are the coordinates of a component and c describes a possible leak test result, with the
following meanings:
Output
Output a single integer, the minimum number of leaking components that could give rise to the observed
leak test results. If no set of leaking components could give rise to the observed results, instead output
the single word impossible.
Example
Input Output
4 1
1 -1 P
2 2 P
-1 3 N
-2 -1 -
2 impossible
0 0 N
1 2 P
Your judges are preparing a problem set, and they’re trying to evaluate a problem for inclusion in the
set. Each judge rates the problem with an integer between −3 and 3, where:
3 means: I really like this problem!
−3 means: I really don’t like this problem!
0 means: Meh. I don’t care if we use this problem or not.
The overall rating of the problem is the average of all of the judges’ ratings–that is, the sum of the ratings
divided by the number of judges providing a rating.
Some judges have already rated the problem. Compute the minimum and maximum possible overall
rating that the problem can end up with after the other judges submit their ratings.
Input
The first line of input contains two integers n (1 ≤ n ≤ 10) and k (0 ≤ k ≤ n), where n is the total
number of judges, and k is the number of judges who have already rated the problem.
Each of the next k lines contains a single integer r (−3 ≤ r ≤ 3). These are the ratings of the k judges
that have already rated the problem
Output
Output two space-separated floating point numbers on a single line, which are the minimum and maximum
overall rating the problem could achieve after the remaining judges rate the problem, minimum first. These
values must be accurate to an absolute error of 10−4
Example
Input Output
5 2 -1.2 2.4
1
2
4 4 -2.75 -2.75
-3
-3
-2
-3
Input
The first line of input consists of an integer n (1 ≤ n ≤ 106 ), the number of songs Tóti has written. The
second line consists of n integers, each in {1, 2, 3}, giving the hype ratings of the n songs in the order they
were written.
Output
Output the number of setlists Tóti can make. Since this number can be large, print it modulo 109 + 7.
Example
Input Output
9 63
1 1 1 2 2 2 3 3 3
8 15
1 2 1 2 3 1 2 3
Input
The input consists of a single line containing the string s (1 ≤ |s| ≤ 50), which represents the initial
arrangement of the cards, in the order they appear in the row. The string contains only lower-case letters
(‘a’-‘z’).
Output
Output a single line with 1 if you can guarantee that you will always be able to guess the audience
member’s operation correctly, or 0 otherwise.
Example
Input Output
robust 1
icpc 0
Problem I. Inquiry II
Source file name: Inquiry.c, Inquiry.cpp, Inquiry.java, Inquiry.py
Input: Standard
Output: Standard
For an undirected, simple graph G = (V, E) we call a subset V 0 ⊆ V an independent set if no two elements
of V 0 are connected by an edge. An independent set of G is called a maximum independent set if there is
no independent set in G with strictly more vertices. Given a specific kind of connected graph G, find the
size of a maximum independent set of G.
Input
• The input starts with one line, containing integers n (1 ≤ n ≤ 100), the number of vertices in the
graph, and m (n − 1 ≤ m ≤ n + 15), the number of edges in the graph.
• Then follow m lines, each containing integers a, b (1 ≤ a, b ≤ n) indicating that there is an edge
between vertices a and b.
The graph given by this input is guaranteed to be both simple and connected: there is at most one edge
between each pair of vertices, there are no loops, and there is a path between each pair of vertices.
Output
Output the number of vertices in a maximum independent set of the input graph.
Example
Input Output
2 1 1
1 2
4 5 2
1 2
2 3
3 4
4 1
1 3
(each faucet produces grade A quality chocolate which instantly mixes with the chocolate from the other
faucets).
Each type of praline and chocolate bar produced at the factory requires the Chocolate River to be adjusted
to have a specific temperature and flow level. Charlie recently came across a long list of new praline recipes,
and would now like to figure out which of these are even possible to make at the factory. Write a program
to determine, for each of the new recipes, if its required temperature and flow level is possible to achieve
with some setting of the k faucets.
Input
The first line of input contains an integer k (1 ≤ k ≤ 10), the number of taps. Then follow k lines,
describing the taps. The i’th of these lines contains the three integers ti , ai , and bi (0 ≤ ti ≤ 106 ,
0 ≤ ai ≤ bi ≤ 106 ) describing the i’th faucet.
Next follows a line containing an integer r (1 ≤ r ≤ 105 ), the number of new recipes to check. Then
follows r lines, each describing a recipe. A recipe is described by two integers τ and φ (0 ≤ τ ≤ 106 and
1 ≤ φ ≤ 106 ), where τ is the chocolate temperature and φ the chocolate flow needed for this recipe.
Output
For each of the r recipes, print one line with the string “yes” if it is possible to achieve the desired
combination of chocolate temperature and flow, and “no” otherwise.
Example
Input Output
2 no
50 0 100 yes
100 50 100 no
3
20 75
75 150
75 90
Input
The input consists of two lines, each containing a positive integer with at most 106 digits. These are the
two integers Petra wants to add.
Output
Output a single integer, the minimum number of times Petra must add 1 to one of her numbers (while
subtracting 1 from the other) until they can be added using the standard addition algorithm without any
carry digits.
Example
Input Output
10 1
99
90 10
10
23425 12085
487915
On a whiteboard, you have found a list of integers. Is it possible to use all of them to write down a correct
arithmetic expression where one of them is the sum of all the others?
You may not alter the integers in any way (e.g., changing the sign or concatenating).
Input
The first line of input contains an integer n (1 ≤ n ≤ 104 ), representing the number of integers on the
whiteboard. The integers on the whiteboard are given over the next n lines, one per line. Their absolute
values are guaranteed to be at most 105 .
Output
Print a single integer x which is one of the inputs, and is the sum of all the others. If there’s more than
one such x, output any one. If there are no such values of x, output the string ‘BAD’.
Example
Input Output
4 6
1
6
3
2
4 0
-2
0
5
-3
5 BAD
1
10
4
2
-3
After one computer crash too many, Alonso has had enough of all
this shoddy software and poorly written code! He decides that in
order for this situation to improve, the glass house that is modern
programming needs to be torn down and rebuilt from scratch using
only completely formal axiomatic reasoning. As one of the first
steps, he decides to implement arithmetic with natural numbers
using the Peano axioms.
The Peano axioms (named after Italian mathematican Giuseppe
Peano) are an axiomatic formalization of the arithmetic properties
of the natural numbers. We have two symbols: the constant 0, and
a unary successor function S. The natural numbers, starting at 0,
are then 0, S(0), S(S(0)), S(S(S(0))), and so on. With these two
symbols, the operations of addition and multiplication are defined
inductively by the following axioms: for any natural numbers x
and y, we have
x+0=x x·0=0
x + S(y) = S(x + y) x · S(y) = x · y + x
The two axioms on the left define addition, and the two on the right define multiplication.
For instance, given x = S(S(0)) and y = S(0) we can repeatedly apply these axioms to derive
Write a program which given two natural numbers x and y, defined in Peano arithmetic, computes the
product x · y.
Input
The input consists of two lines. Each line contains a natural number defined in Peano arithmetic, using
at most 1 000 characters.
Output
Output the product of the two input numbers.
Example
Input Output
S(S(0)) S(S(S(S(S(S(0))))))
S(S(S(0)))
S(S(S(S(S(0))))) 0
0
You are teaching kindergarten! You wrote down the numbers from 1 to n, in order, on a whiteboard.
When you weren’t paying attention, one of your students erased one of the numbers.
Can you tell which number your mischievous student erased?
Input
The first line of input contains a single integer n (2 ≤ n ≤ 100), which is the number of numbers that
you wrote down.
The second line of input contains a string of digits, which represents the numbers you wrote down (minus
the one that has been erased). There are no spaces in this string. It is guaranteed to contain all of the
numbers from 1 to n, in order, except for the single number that the student erased.
Output
Output a single integer, which is the number that the tricky student erased.
Example
Input Output
5 4
1235
10 7
1234568910
15 11
1234567891012131415