Professional Documents
Culture Documents
FARIDA - Princess Farida: Input
FARIDA - Princess Farida: Input
FARIDA - Princess Farida: Input
#dynamic-programming
Once upon time there was a cute princess called Farida living in a castle with her father, mother
and uncle. On the way to the castle there lived many monsters. Each one of them had some gold
coins. Although they are monsters they will not hurt. Instead they will give you the gold coins, but
if and only if you didn't take any coins from the monster directly before the current one. To marry
princess Farida you have to pass all the monsters and collect as many coins as possible. Given
the number of gold coins each monster has, calculate the maximum number of coins you can
collect on your way to the castle.
Input
The first line of input contains the number of test cases. Each test case starts with a number N,
the number of monsters, 0 <= N <= 10^4. The next line will have N numbers, number of coins
each monster has, 0 <= The number of coins with each monster <= 10^9. Monsters described in
the order they are encountered on the way to the castle.
Output
For each test case print Case C: X without quotes. C is the case number, starting with 1. X is
the maximum number of coins you can collect.
Example
Input:
2
5
12345
1
10
Output:
Case 1: 9
Case 2: 10
PIGBANK - Piggy-Bank
#dynamic-programming
Before ACM can do anything, a budget must be prepared and the necessary financial support
obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea
behind is simple. Whenever some ACM member has any small money, he takes all the coins and
throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be
removed without breaking the pig. After a sufficiently long time, there should be enough cash in
the piggy-bank to pay everything that needs to be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is
inside. So we might break the pig into pieces only to find out that there is not enough money.
Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank
and try to guess how many coins are inside. Assume that we are able to determine the weight of
the pig exactly and that we know the weights of all coins of a given currency. Then there is some
minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this
worst case and determine the minimum amount of cash inside the piggy-bank. We need your
help. No more prematurely broken pigs!
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input
file. Each test case begins with a line containing two integers E and F. They indicate the weight
of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh
more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there
is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given
currency. Following this are exactly N lines, each specifying one coin type. These lines contain
two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in
monetary units, W is it's weight in grams.
Output
Print exactly one line of output for each test case. The line must contain the sentence "The
minimum amount of money in the piggy-bank is X." where X is the minimum amount of money
that can be achieved using coins with the given total weight. If the weight cannot be reached
exactly, print a line "This is impossible.".
Example
Sample Input:
3
10 110
2
11
30 50
10 110
2
11
50 30
16
2
10 3
20 4
Sample output:
1111110
72
3213210
71
7654320
Sample Output:
Scenario #1:
Teresa will skip 4 strawberries
Scenario #2:
Teresa will skip 0 strawberries
Scenario #3:
Teresa will skip all the strawberries
Explanation of the second sample test:
Teresa takes the first strawberry at the 1st tile, moves 3 tiles to the right (4th tile), then she grabs
the strawberry (3) and moves 3 more tiles, arriving then to the tile 7 where is the pot.
CONSTRAINTS:
2<=N<=1000
0<=K<=N
1<=Ni<=N
Input
The input will contain several test cases (not more than 10). Each testcase is a single line with a
number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
Output
For each test case output a single line, containing the maximum amount of American dollars you
can make.
Example
Input:
12
2
Output:
13
2
You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try
changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more
than $1 out of them. It is better just to change the 2 coin directly into $2.
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast
amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a
given period time. The treats are interesting for many reasons:
The treats are numbered 1..N and stored sequentially in single file in a long box that is
open at both ends. On any day, FJ can retrieve one treat from either end of his stash of
treats.
Like fine wines and delicious cheeses, the treats improve with age and command greater
prices.
The treats are not uniform: some are better and have higher intrinsic value. Treat i has
value v(i) (1 <= v(i) <= 1000).
Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.
Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the
greatest value FJ can receive for them if he orders their sale optimally?
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
Input
Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Output
The maximum revenue FJ can achieve by selling the treats
Example
Input:
5
1
3
1
5
2
Output:
43
He starts by choosing any tile in the first row, and collects the philosophers stones on
that tile. Then, he moves to a tile in the next row, collects the philosophers stones on the
tile, and so on until he reaches the last row.
When he moves from one tile to a tile in the next row, he can only move to the tile just
below it or diagonally to the left or right.
Given the values of h and w, and the number of philosophers stones on each tile, write a
program to compute the maximum possible number of philosophers stones Harry can grab in
one single trip from the first row to the last row.
Input
The first line consists of a single integer T, the number of test cases. In each of the test cases,
the first line has two integers. The first integer h (1<=h<=100) is the number of rows of tiles on
the floor. The second integer w (1<=w<=100) is the number of columns of tiles on the floor. Next,
there are h lines of inputs. The ith line of these, specifies the number of philosophers stones in
each tile of the ith row from the front. Each line has w integers, where each integer m
(0<=m<=100) is the number of philosophers stones on that tile. The integers are separated by a
space character.
Output
The output should consist of T lines, (1<=T<=100), one for each test case. Each line consists of
a single integer, which is the maximum possible number of philosophers stones Harry can grab,
in one single trip from the first row to the last row for the corresponding test case.
Example
Input:
1
65
31742
21311
12218
22153
21444
52751
Output:
32
//7+1+8+5+4+7=32
ACODE - Alphacode
#dynamic-programming
Alice and Bob need to send secret messages to each other and are discussing ways to encode
their messages:
Alice: Lets just use a very simple code: Well assign A the code word 1, B
will be 2, and so on down to Z being assigned 26.
Bob: Thats a stupid code, Alice. Suppose I send you the word BEAN
encoded as 25114. You could decode that in many different ways!
Alice: Sure you could, but what words would you get? Other than BEAN,
youd get BEAAD, YAAD, YAN, YKD and BEKD. I think you would be able
to figure out the correct decoding. And why would you send me the word
BEAN anyway?
Bob: OK, maybe thats a bad example, but I bet you that if you got a string of
length 5000 there would be tons of different decodings and with that many you
would find at least two different ones that would make sense.
Alice: How many different decodings?
Bob: Jillions!
For some reason, Alice is still unconvinced by Bobs argument, so she requires a program that
will determine how many decodings there can be for a given string using her code.
Input
Input will consist of multiple input sets. Each set will consist of a single line of at most 5000 digits
representing a valid encryption (for example, no line will begin with a 0). There will be no spaces
between the digits. An input line of 0 will terminate the input and should not be processed.
Output
For each input set, output the number of possible decodings for the input string. All answers will
be within the range of a 64 bit signed integer.
Example
Input:
25114
1111111111
3333333333
0
Output:
6
89
1