Problem A. Ladder: Input File Format
Problem A. Ladder: Input File Format
Problem A. Ladder
The ladder has n stairs, indexed by numbers 1, 2, . . . , n from bottom to top. There is a
number written on each stair. You want to climb from the bottom (stair with index 0) to the
very top (stair with index n) of the ladder. In one step, you can step up either one or two
stairs. As you climb, you add up the numbers on the stairs you step on. You want to maximize
this sum.
Sample tests
ladder.in ladder.out
2 3
1 2
2 1
2 -1
3 3
-1 2 1
Page 1 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Problem B. Rabbit
The rabbit jumps along the line. There are n cells on this line, indexed by numbers from 1
to n. Some cells are swamps, and the rabbit cannot land into them. Other cells contain tasty
grass, a rabbits’ beloved snack.
The rabbit starts at the cell with index 1 and wants to get to the cell with index n. It wants
to eat as much grass along the way as possible, without landing into the swamp. The rabbit’s
design features mean that it can only jump 1, 3, and 5 cells to the right.
Determine what is the maximum number of cells with grass that a rabbit can visit.
Sample tests
ladder.in ladder.out
4 2
."".
5 0
.w"..
9 -1
.www.www.
Page 2 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
You are given a rectangular board N × M , with a chess knight in the top left corner. The
knight can only move downwards, and to the right, that’s it:
Find the number of different ways the knight can take to reach the bottom-right corner of
the board.
Sample tests
knight.in knight.out
3 2 1
31 34 293930
Page 3 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Every square of an 8 × 8 chessboard has a non-negative number written on it. The king
wants to move from the lower-left corner of the board to the upper right corner, but it can only
walk upwards, to the right, and diagonally to the upper right. The cost of visiting a square is
equal to the number written on it.
Move the knight from the lower-left corner of the board to the upper right corner with
minimum path cost.
Sample tests
king2.in king2.out
9 9 9 9 9 9 1 9 56
9 9 9 9 9 1 9 2
9 9 9 9 9 9 1 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9
0 9 9 9 9 9 9 9
Page 4 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Problem E. Slalom
One of the popular ski resorts in Italy hosts a slalom competition. Every participant will
go down the mountain on the skis. On every path of the path, the participant will get some
number of points. As the participant goes down, we add these points together. The participant
with the maximum number of points wins. The mountain is a triangle of integers, representing
points for parts of the path. Participants may choose whether to go left or right on every layer
as they go down to the next layer. The participants start in the highest number and may finish
in any of the lowest.
For example, the mountain can look like this:
1
4 3
5 6 7
8 9 0 9
Sample tests
slalom.in slalom.out
4 20
1
4 3
5 6 7
8 9 0 9
Page 5 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Problem F. Cannonballs
Captain Vasya always keeps some cannonballs on his ship if he ever needs to fight with the
pirates. He stores the cannonballs in the pyramids. Each layer of the pyramid is an equilateral
triangle of side k filled with cannonballs. The lowest layer of the pyramid has side n, the next
layer has n − 1, . . . , and the last layer is a single cannonball, i.e. the triangle of side 1.
For example, a pyramid of size 3 consists of the following layers (from top to bottom):
X
X X
X
X X
X X X
Sample tests
balls.in balls.out
5 1
1 2
5 3
9 3
15 2
91
Page 6 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Sample tests
longpath.in longpath.out
5 5 3
1 2
2 3
3 4
3 5
1 5
Page 7 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
We call a set of positive integers peaceful, if any two of its elements differ at least two times.
We call the sum of the elements of the set its strength. Find the number of peaceful sets of a
given strength.
Sample tests
peacefulsets.in peacefulsets.out
2 1
5 2
Page 8 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Problem I. Knapsack
You came across a buried treasure, consisting of N golden ingots with given weights. Find
the maximum total mass of gold that you can take if you only have a knapsack of capacity S.
Sample tests
knapsack.in knapsack.out
10 3 9
1 4 8
20 4 19
5 7 12 18
Page 9 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
Find the number of strings consisting of letters ’a’, ’b’, ’c’ of the given length n and
without a substring "ab".
Sample tests
ladder.in ladder.out
0 1
3 21
11 46368
Page 10 of 11
Saint Petersburg State University
2012-2013 Group C Problemset 5 Dynamic Programming
N children are standing in the circle. They start counting clockwise. Whenever someone
gets number P , they leave the circle, the counter is reset, and counting contains from their
right neighbour.
The last man standing wins.
Find the winning number (children are indexed 1 to N , starting from the same child that
the count begins from).
Sample tests
joseph.in joseph.out
3 4 2
Page 11 of 11