TAMU Team Programming Contest TAMU, February, 24 2018 Problem A. Arya Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Arya Stark wants to take dancing lessons1. Given how much she loves “dancing,” she wants to practice with her dancing master as much as possible. Her dancing master will be around for n days, and he will be available for training some h number of hours each day. As Arya usually has to skip some important banquet or tournament in order to practice, her father will only let her practice on k of the n days. How many hours can Arya practice in total, if she chooses her days optimally? Input The first line of input consists of two integers, n and k (1≤k≤100), representing the number of days Arya’s dancing master is available, and the number of days Arya is allowed to practice. Each of the next n lines contains a number h (1≤h≤10) describing the number of hours of the given practice. Output Output a single integer giving the maximum number of hours Arya can spend practicing. Example standard input standard output 7 3 23 6 1 2 10 2 7 5 Note 1: They’re actually sword fighting lessons, but that’s a secret kept between Arya and her father In the first test case, since Arya can practice on 3 days, she will choose the first, fourth, and sixth days, for a total of 6+10+7=23 hours of practice.

Page 1 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem B. Bran Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Bran Stark loves climbing the towers of Winterfell. The towers are arranged in a rectangular grid of m rows and n columns, with each tower having a different height h units. Bran starts at the tower in the top left corner. From a given position, Bran can climb to any adjacent (but not diagonal) tower, as long as the height of the adjacent tower is within k units of the height of his current tower. Given the layout of the towers of Winterfell, determine the maximum height to which Bran can climb. Input The first line of input contains three integers: m, n, and k, representing the number of rows on the grid, the number of columns on the grid, and the height difference which Bran can climb (1≤m, n, k≤100). The following m lines each contain n integers, representing the height (1≤h≤100) of each tower on the grid. Output Output one integer, containing the maximum height to which Bran can climb. Examples standard input standard output 3 4 2 8 3 1 2 4 5 9 6 5 7 5 8 2 3 2 10 25 25 36 12 21 18 27 Note In the first test case, Bran starts his journey on the tower of height 3. He climbs east, east, east, south, west, then south, to reach the tower of height 8. It is impossible for Bran to climb to the tower of height 9, because it is a minimum of 3 units away from the adjacent towers, which is greater than the k of 2. In the second test case, Bran starts at the tower with the height of 25 and cannot climb to either of the two adjacent towers, which have a height difference greater than the k of 10.

Page 2 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem C. Cersei Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Cersei Lannister, queen of the Seven Kingdoms, has encountered something of a predicament. Due to the careless spending of her husband1, the crown has taken on a substantial amount of debt. always pays her debts. The crown has received loans from the Iron Bank of Braavos, each with a fixed interest rate. An interest rate of r means that for every 100 gold pieces the crown owes, its debt increases by an additional r gold pieces each year. As the Iron Bank of Braavos likes to be very accurate, they keep track of their loans with perfect precision, meaning that balances can be decimal values. Given the amounts and dates of the loans the crown has received, determine its total amount of debt. Input The first line of input consists of two integers, n and r (1≤n, r≤10), representing the number of loans the crown has received and the interest rate throughout the Seven Kingdoms. Each of the next n lines contains two positive integers no greater than 100, containing the original amount of money in the loan and the number of years ago it was made. Output Output a floating point number containing the crown’s current amount of debt. Your answer will be considered correct if its absolute or relative accuracy is within 0.0001 of the correct answer. Example standard input standard output 3 5 284.812567266 100 7 40 1 80 5 Note 1: King Robert’s main expenditure is prize money for tournaments, which he loves to watch. He doesn’t really have any other vices.

Page 3 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem D. Daenerys Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

In celebration of her recent marriage, has been given dragon eggs as a gift! Since no one has ever been able to hatch a dragon in over a century, the eggs were given to her merely as a decoration. But Daenerys has other plans. Daenerys knows that dragon eggs will hatch when they’re set on fire, just as long as the eggs are old enough. Specifically, they will hatch if and only if they are at least p years old. Given the age of each of Daenerys’s dragon eggs, determine how many of them are old enough to hatch, once they’re set on fire. Input The first line of input consists of two integers, n and p (1≤n, p≤100), representing the number of dragon eggs and the age at which dragon eggs hatch. Each of the next n lines contains a number k (1≤k≤100) describing the age of a given dragon egg. Output Output a single integer giving the number of dragons that will hatch from the dragon eggs. Example standard input standard output 3 50 2 60 40 50

Page 4 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem E. Eddard Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Lord Eddard Stark, son of Rickard Stark, Lord of Winterfell and Warden of the North, has been feeling rather cold lately. He recalls the old words of House Stark, “Winter is coming,” and wonders whether or not this is the case. For the past few days, Lord Stark has kept track of whether he felt warm or cold. If Lord Stark has felt cold three days in a row, it must be the case that winter is coming. Otherwise, it will be another year of summer. Can you determine which outcome is true? Input The first line of input is the number of temperature recordings, n (3≤n≤10). Each of the next n lines contains one lowercase word describing the temperature: “warm” or “cold”. Output If there are three consecutive cold days in the recordings, then print “Winter is coming”. If not, then print “Another year of summer”. Examples standard input standard output 5 Winter is coming warm cold cold cold warm 7 Another year of summer cold cold warm cold cold warm cold

Page 5 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem F. Free Folk Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Far north of the seven kingdoms are the lands beyond the Wall, where the Free Folk live. For thousands of years, the Free Folk have warred against the men of the Night’s Watch, to little avail. But recently, the Free Folk united under the king beyond the wall, who will march them to their victory. Their army is quite formidable, consisting of three types of soldiers: humans, giants, and mammoths. The king beyond the wall has ordered a specific battle formation for the Free Folk. The Free Folk will organize themselves in a rectangle with a width of 3 units and a length of n units. Their formation can include any number of humans, giants, and mammoths. A human takes up a 1x1 square, a giant takes up a 2x2 square, and a mammoth takes up a 3x3 square. Every square in the grid must be filled. The king beyond the wall would like to know how many possible configurations of his army exist. Two configurations are considered different if any given position between them has a different type of soldier. As the number of possible configurations can be very large, it should be output modulo 1000000007 (109+7). Input Input consists of a single integer, n (1≤n≤106), representing the length of the formation. Output Output a single integer giving the number of possible configurations of the army, modulo 1000000007 (109+7). Example standard input standard output 4 13 Note In the first test case n = 4 and the output is 13. All 13 possibilities are shown below.

Page 6 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem G. Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Gendry, a strong young man of questionable parentage1, works in an armory. He’s just received a big shipment of p pounds of steel and must decide what to do with it. There are three different things he can do with the steel. 1. Use Ap pounds of steel to create a suit of armor, which he can sell for Ag gold pieces. 2. Use Sp pounds of steel to create a sword, which he can sell for Sg gold pieces. 3. Sell one pound of steel to another blacksmith for g gold pieces. Gendry can perform any amount and combination of the above three actions, but he would like to make as much gold as possible. How much gold can he earn? Input Input consists of one line with six space-separate nonzero integers, p, Ap, Ag, Sp, Sg, g. Their significance is as follows. p gives the amount of steel available. Ap gives the amount of steel required to make a suit of armor. Ag gives the value in gold of a suit of armor. Sp gives the amount of steel required to make a sword. Sg gives the value in gold of a sword. g gives the value in gold of each pound of pure steel. No number will be greater than 1000. Output Output a single integer giving the maximum amount of gold pieces that Gendry can make from his steel. Example standard input standard output 31 13 40 8 24 2 92 Note 1. His physique and strength resemble that of a younger King Robert, but that’s probably just a coincidence. In the first test case, the optimal combination is for Gendry to sell 1 suit of armor, 2 swords, and 2 pounds of steel.

Page 7 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem H. Hodor Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Hodor enjoys saying his name, Hodor. He has a series of word fragments in his head, which he would like to rearrange and concatenate, such that the phrase produced includes as many occurrences of Hodor as possible. For example, if his word fragments are odordr, rHodo, and roHdH, he can arrange them in order of second, third, first, such that the resulting string is rHodoroHdHodordr. This string contains two occurrences of Hodor, which is the maximum out of any possible arrangement. Given the word fragments Hodor has in his head, what is the maximum number of times he can say Hodor? Input The first line of input contains one integer n, representing the number of fragments (1≤n≤15). The following n lines each contain one string of x characters (5≤x≤1000). Strings will consist only of the following four characters: ‘H‘, ‘o‘, ‘d‘, ‘r‘. Output Output one integer, containing the maximum number of occurrences of Hodor if the strings are concatenated optimally. Examples standard input standard output 3 2 odordr rHodo roHdH 4 6 rrodo orroHodorHdHodoroHod HHHod orodHHodorrHodo

Page 8 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem I. Iron Throne Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Hundreds of years ago, King Aegon conquered Westeros and forged the Iron Throne with the swords of his fallen enemies. Ever since Aegon’s conquest, some number n of kings have sat atop the Iron Throne, each ruling for some number of years, but with only one king ruling in each given year. The Maesters of the Citadel have maintained a record of each new king, along with the year when they began their rule. A given king i began their rule in year yi and continued their rule until year yi+1, when king i + 1 became the new king. The Maesters have some number of queries q regarding exactly which king was on the Iron Throne during a given year. Your task is to provide them with this information. Input The first line contains two integers, n and q representing the number of kings and the number of queries. The following n lines contain a string s and a unique integer x, representing the name of the king and the year when they became king. The following q lines consist of an integer y, representing the year which the Maesters would like to know who the king was. Kings will be provided in increasing order by year when they became king, with no two kings having the same year. You may assume that the very first king (who began his rule in year 0) was named “Aegon”. All integer inputs will be from 1 to 1000, and all string inputs will consist of up to 15 capital and lowercase letters. Output For each of the q queries, output the name of the king corresponding to the given year. Example standard input standard output 5 6 Baelor Maegor 42 Maegor Viserys 103 Aegon Baelor 161 Baelor Aerys 262 Viserys Robert 283 Aegon 187 102 1 161 134 30

Page 9 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem J. Jon Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Jon Snow is a sworn brother of the Night’s Watch, an ancient order sworn to guard the Wall, protecting the Seven Kingdoms from the enemies lurking beyond. As recruitment for the Night’s Watch has dropped in recent years, the Lord Commander has asked Jon for advice on how to best protect the Wall. There are many castles at the Wall, each with some number of men stationed at them. Unfortunately, each castle does not necessarily have enough men remaining to guard it. If a castle does not have enough men, it will have to be abandoned. In order to seal off as few castles as possible, Jon may move any number of men to an immediately adjacent castle. For example, men in the 5th castle could be moved to the 4th castle or the 6th castle, but not any of the others. Jon may perform this action on multiple castles, but only with the number of men that the castle started with. If Jon makes his decisions optimally, what is the fewest number of castles that must be abandoned? Input The first line of input consists of two integers, n and p (1≤n, p≤100), representing the number of castles and the number of men needed to guard each castle. Each of the next n lines contains a number k (0≤k≤1000) describing the number of men currently stationed at that castle. Output Output a single integer giving the minimum number of castles that must be abandoned. Example standard input standard output 5 20 1 0 100 10 10 0 Note In the first test case, Jon would move 20 men from Castle 2 to Castle 1, 20 men from Castle 2 to Castle 3, and 10 men from Castle 3 to Castle 4. Castle 5 could not have enough men and would have to be sealed off.

Page 10 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem K. King in the North Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

The tragic death of (spoiler alert) Eddard Stark has spurred the Northern lords to declare his son, , the King in the North. Robb’s first task as King is to define the exact boundaries of his new northern kingdom. The continent of Westeros is approximated as a rectangle of width w and height h. The various towns in the North, approximated as points inside the rectangle, must be included within the borders of the North. To keep matters simple, the King in the North will make his kingdom trapezoidally-shaped: the Northern border is the horizontal top edge of the continent, the eastern and western borders are the vertical sides of the continent, and the southern border is a horizontal or diagonal line between the eastern and western borders. The King in the North can draw this line anywhere such that every town in the North lies on or above the line (such that the y-coordinate of the town is greater than the y-coordinate of the border), but he would like the overall area of the North to be as small as possible, such that it can be defended from raids more easily. Given the dimensions of the rectangular continent and the location of every town in the North, determine the minimum possible area for the North. Input The first line of input consists of three integers, n, w, and h (1≤n, w, h≤10000), representing the number of towns in the North, the width of the continent, and the height of the continent. Each of the next n lines contains two integers, x (0≤x≤w) and y (0≤y≤h), providing the coordinates of each town. Output Output a floating point number containing the minimum area for which the North can be defined. Your answer will be considered correct if its absolute or relative accuracy is within 0.0001 of the correct answer. Example standard input standard output 7 4 6 10 1 4 1 5 2 4 2 6 3 3 3 5 4 3 Note In the first test case, the northern border is a horizontal line of 4 units. The eastern border is a vertical line of 1.5 units. The western border is a vertical line of 3.5 units. The southern border is a diagonal line from (0,4.5) to (4,2.5). The total area of the trapezoid is 10. The first image below shows the Northern kingdom represented as points, and the second image shows the optimal borders.

Page 11 of 15 TAMU Team Programming Contest TAMU, February, 24 2018

Page 12 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem L. Littlefinger Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

A tournament is to be held between all the most honorable knights in the land. Lord , also known as Littlefinger, is not a knight, nor is he honorable, but he still enjoys tournaments. He particularly enjoys gambling on tournaments - it’s how he’s made his fortune1. Having spectated many tournaments before, Littlefinger usually knows which knight will be victorious in a given joust. Knights can be either Strong, Skilled, Experienced, Chivalrous, or Sneaky. A Strong knight will always defeat a Skilled or a Sneaky knight. A Skilled knight will always defeat a an Experienced or Sneaky knight. An Experienced knight will always defeat a Strong or Chivalrous knight. A Chivalrous knight will always defeat a Strong or Skilled knight. And lastly, a Sneaky knight will always defeat a Chivalrous or Experienced knight. If two knights of same characteristic face each other, either of them could win. Each round of the tournament, knights are paired off sequentially in groups of two. The first knight on the list plays the second knight, while the third knight plays the fourth knight, and so on. After each round, the defeated knights are removed from the list, and the knights are paired again. Every remaining knight will have an opponent each round until only one knight remains, and this knight will be declared the winner. Input The first line of input contains a number n (1≤n≤1024, n=2x, where x is an integer), representing the number of knights in the tournament. The following n lines contain a string representing the name of the knight, and a string representing the knight’s characteristic. Every string consists of up to 15 capital and lowercase letters. The knight’s characteristic will be one of the following: Strong, Skilled, Experienced, Chivalrous, or Sneaky, with any capitalization. Output If it is possible to know the winner of the tournament with certainty, output the name of the knight who will win. Otherwise, output the phrase, “Chaos is a ladder”. Examples standard input standard output 4 Loras Sandor skilled Loras Chivalrous Gregor STRONG Hugh sNEAKY 4 Chaos is a ladder Jaime Experienced Barristan Experienced Meryn Strong Renly Chivalrous

Note 1. Alongside other similarly lucrative pursuits

Page 13 of 15 TAMU Team Programming Contest TAMU, February, 24 2018 Problem M. Margaery Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes

Following the unfortunate death of King Robert in a tragic hunting “accident,” various Princes and Lords are claiming the title of King for themselves. Specifically, there are five of these new Kings, hence why the war over King Robert’s succession is being called the War of the Five Kings. Lady Margaery Tyrell would like to know which of the five kings has the largest army. Unfortunately, the situation is somewhat complicated. Each king’s army is composed of the armies of the houses who have pledged their support. Some houses, in addition to their own army, also have the support of other houses. These houses could, in turn, have the support of other houses, and so on. Thus, it can be very difficult to calculate the size of each king’s army. Can you help Margaery determine which king has the largest army? Input The first line of input contains five strings, giving the names of the five kings. The next line contains a number n (1≤n≤10000), representing the number of noble houses. Each of the next n lines describes the house. The line begins with a string, providing the name of the house. This is followed by a number x (1≤x≤10000), giving the size of the house’s army. Lastly, the line contains one string containing either the house or the king to which this house is loyal. Every string consists of up to 15 capital and lowercase letters. Output Output a single string, giving the name of the king with the largest army under his control. There will always be a king with an army strictly larger than the other four. Example standard input standard output Joffrey Renly Stannis Robb Balon Renly 13 Stark 5000 Robb Lannister 60000 Joffrey Frey 5000 Tully Tarth 10000 Baratheon Tyrell 25000 Renly Greyjoy 4000 Balon Tarly 10000 Tyrell Baratheon 20000 Renly Tully 20000 Robb Dragonstone 5000 Stannis Bolton 4000 Stark Umber 3000 Stark Erenford 1000 Frey

Note Joffrey’s army is made up of house Lannister (pledged to Joffrey). Renly’s army is made up of houses Baratheon (pledged to Renly), Tarth (pledged to Baratheon), Tyrell (pledged to Renly), and Tarly (pledged to Tyrell). Stannis’s army is made up of Dragonstone (pledged to Stannis). Robb’s army is made up of houses Stark (pledged to Robb), Bolton (pledged to Stark), Umber (pledged to Stark), Tully

Page 14 of 15 TAMU Team Programming Contest TAMU, February, 24 2018

(pledged to Robb), Frey (pledged to Tully), and Erenford (pledged to Frey). Balon’s army is made up of house Greyjoy (pledged to Balon). The size of the armies of Joffrey, Renly, Stannis, Robb, and Balon are 60000, 65000, 5000, 38000, and 4000, respectively. Thus, Renly’s army of 65000 is the largest.

Page 15 of 15