Dynamic Programming Jul 06, 2025
Problem A. Dice Combinations
Time Limit 1000 ms
Mem Limit 524288 kB
Your task is to count the number of ways to construct sum n by throwing a dice one or
more times. Each throw produces an outcome between 1 and 6.
For example, if n = 3, there are 4 ways:
1+1+1
1+2
2+1
3
Input
The only input line has an integer n.
Output
Print the number of ways modulo 109 + 7.
Constraints
1 ≤ n ≤ 106
Example
Input Output
3 4
Page 1 of 23
-
Dynamic Programming Jul 06, 2025
Problem B. Minimizing Coins
Time Limit 1000 ms
Mem Limit 524288 kB
Consider a money system consisting of n coins. Each coin has a positive integer value.
Your task is to produce a sum of money x using the available coins in such a way that the
number of coins is minimal.
For example, if the coins are {1, 5, 7} and the desired sum is 11, an optimal solution is
5 + 5 + 1 which requires 3 coins.
Input
The first input line has two integers n and x: the number of coins and the desired sum of
money.
The second line has n distinct integers c1 , c2 , … , cn : the value of each coin.
Output
Print one integer: the minimum number of coins. If it is not possible to produce the
desired sum, print −1.
Constraints
1 ≤ n ≤ 100
1 ≤ x ≤ 106
1 ≤ ci ≤ 106
Example
Input Output
3 11 3
1 5 7
Page 2 of 23
-
Dynamic Programming Jul 06, 2025
Problem C. Coin Combinations I
Time Limit 1000 ms
Mem Limit 524288 kB
Consider a money system consisting of n coins. Each coin has a positive integer value.
Your task is to calculate the number of distinct ways you can produce a money sum x
using the available coins.
For example, if the coins are {2, 3, 5} and the desired sum is 9, there are 8 ways:
2+2+5
2+5+2
5+2+2
3+3+3
2+2+2+3
2+2+3+2
2+3+2+2
3+2+2+2
Input
The first input line has two integers n and x: the number of coins and the desired sum of
money.
The second line has n distinct integers c1 , c2 , … , cn : the value of each coin.
Output
Print one integer: the number of ways modulo 109 + 7.
Constraints
1 ≤ n ≤ 100
1 ≤ x ≤ 106
1 ≤ ci ≤ 106
Example
Input Output
3 9 8
2 3 5
Page 3 of 23
-
Dynamic Programming Jul 06, 2025
Problem D. Coin Combinations II
Time Limit 1000 ms
Mem Limit 524288 kB
Consider a money system consisting of n coins. Each coin has a positive integer value.
Your task is to calculate the number of distinct ordered ways you can produce a money
sum x using the available coins.
For example, if the coins are {2, 3, 5} and the desired sum is 9, there are 3 ways:
2+2+5
3+3+3
2+2+2+3
Input
The first input line has two integers n and x: the number of coins and the desired sum of
money.
The second line has n distinct integers c1 , c2 , … , cn : the value of each coin.
Output
Print one integer: the number of ways modulo 109 + 7.
Constraints
1 ≤ n ≤ 100
1 ≤ x ≤ 106
1 ≤ ci ≤ 106
Example
Input Output
3 9 3
2 3 5
Page 4 of 23
-
Dynamic Programming Jul 06, 2025
Problem E. Removing Digits
Time Limit 1000 ms
Mem Limit 524288 kB
You are given an integer n. On each step, you may subtract one of the digits from the
number.
How many steps are required to make the number equal to 0?
Input
The only input line has an integer n.
Output
Print one integer: the minimum number of steps.
Constraints
1 ≤ n ≤ 106
Example
Input Output
27 5
Explanation: An optimal solution is 27 → 20 → 18 → 10 → 9 → 0.
Page 5 of 23
-
Dynamic Programming Jul 06, 2025
Problem F. Grid Paths
Time Limit 1000 ms
Mem Limit 524288 kB
Consider an n × n grid whose squares may have traps. It is not allowed to move to a
square with a trap.
Your task is to calculate the number of paths from the upper-left square to the lower-
right square. You can only move right or down.
Input
The first input line has an integer n: the size of the grid.
After this, there are n lines that describe the grid. Each line has n characters: . denotes
an empty cell, and * denotes a trap.
Output
Print the number of paths modulo 109 + 7.
Constraints
1 ≤ n ≤ 1000
Example
Input Output
4 3
....
.*..
...*
*...
Page 6 of 23
-
Dynamic Programming Jul 06, 2025
Problem G. Book Shop
Time Limit 1000 ms
Mem Limit 524288 kB
You are in a book shop which sells n different books. You know the price and number of
pages of each book.
You have decided that the total price of your purchases will be at most x. What is the
maximum number of pages you can buy? You can buy each book at most once.
Input
The first input line contains two integers n and x: the number of books and the maximum
total price.
The next line contains n integers h1 , h2 , … , hn : the price of each book.
The last line contains n integers s1 , s2 , … , sn : the number of pages of each book.
Output
Print one integer: the maximum number of pages.
Constraints
1 ≤ n ≤ 1000
1 ≤ x ≤ 105
1 ≤ hi , si ≤ 1000
Example
Input Output
4 10 13
4 8 5 3
5 12 8 1
Explanation: You can buy books 1 and 3. Their price is 4 + 5 = 9 and the number of pages
is 5 + 8 = 13.
Page 7 of 23
-
Dynamic Programming Jul 06, 2025
Problem H. Array Description
Time Limit 1000 ms
Mem Limit 524288 kB
You know that an array has n integers between 1 and m, and the absolute difference
between two adjacent values is at most 1.
Given a description of the array where some values may be unknown, your task is to count
the number of arrays that match the description.
Input
The first input line has two integers n and m: the array size and the upper bound for each
value.
The next line has n integers x1 , x2 , … , xn : the contents of the array. Value 0 denotes an
unknown value.
Output
Print one integer: the number of arrays modulo 109 + 7.
Constraints
1 ≤ n ≤ 105
1 ≤ m ≤ 100
0 ≤ xi ≤ m
Example
Input Output
3 5 3
2 0 2
Explanation: The arrays [2, 1, 2], [2, 2, 2] and [2, 3, 2] match the description.
Page 8 of 23
-
Dynamic Programming Jul 06, 2025
Problem I. Counting Towers
Time Limit 1000 ms
Mem Limit 524288 kB
Your task is to build a tower whose width is 2 and height is n. You have an unlimited
supply of blocks whose width and height are integers.
For example, here are some possible solutions for n = 6:
Given n, how many different towers can you build? Mirrored and rotated towers are
counted separately if they look different.
Input
The first input line contains an integer t: the number of tests.
After this, there are t lines, and each line contains an integer n: the height of the tower.
Output
For each test, print the number of towers modulo 109 + 7.
Constraints
1 ≤ t ≤ 100
1 ≤ n ≤ 106
Example
Input Output
3 8
2 2864
6 640403945
1337
Page 9 of 23
-
Dynamic Programming Jul 06, 2025
Problem J. Edit Distance
Time Limit 1000 ms
Mem Limit 524288 kB
The edit distance between two strings is the minimum number of operations required to
transform one string into the other.
The allowed operations are:
Add one character to the string.
Remove one character from the string.
Replace one character in the string.
For example, the edit distance between LOVE and MOVIE is 2, because you can first
replace L with M, and then add I.
Your task is to calculate the edit distance between two strings.
Input
The first input line has a string that contains n characters between A–Z.
The second input line has a string that contains m characters between A–Z.
Output
Print one integer: the edit distance between the strings.
Constraints
1 ≤ n, m ≤ 5000
Example
Input Output
LOVE 2
MOVIE
Page 10 of 23
-
Dynamic Programming Jul 06, 2025
Problem K. Longest Common Subsequence
Time Limit 1000 ms
Mem Limit 524288 kB
Given two arrays of integers, find their longest common subsequence.
A subsequence is a sequence of array elements from left to right that can contain gaps. A
common subsequence is a subsequence that appears in both arrays.
Input
The first line has two integers n and m: the sizes of the arrays.
The second line has n integers a1 , a2 , … , an : the contents of the first array.
The third line has m integers b1 , b2 , … , bm : the contents of the second array.
Output
First print the length of the longest common subsequence.
After that, print an example of such a sequence. If there are several solutions, you can
print any of them.
Constraints
1 ≤ n, m ≤ 1000
1 ≤ ai , bi ≤ 109
Example
Input Output
8 6 3
3 1 3 2 7 4 8 2 1 2 4
6 5 1 2 3 4
Page 11 of 23
-
Dynamic Programming Jul 06, 2025
Problem L. Rectangle Cutting
Time Limit 1000 ms
Mem Limit 524288 kB
Given an a × b rectangle, your task is to cut it into squares. On each move you can select a
rectangle and cut it into two rectangles in such a way that all side lengths remain integers.
What is the minimum possible number of moves?
Input
The only input line has two integers a and b.
Output
Print one integer: the minimum number of moves.
Constraints
1 ≤ a, b ≤ 500
Example
Input Output
3 5 3
Page 12 of 23
-
Dynamic Programming Jul 06, 2025
Problem M. Minimal Grid Path
Time Limit 1000 ms
Mem Limit 524288 kB
You are given an n × n grid whose each square contains a letter.
You should move from the upper-left square to the lower-right square. You can only
move right or down.
What is the lexicographically minimal string you can construct?
Input
The first line has an integer n: the size of the grid.
After this, there are n lines that describe the grid. Each line has n letters between A and
Z.
Output
Print the lexicographically minimal string.
Constraints
1 ≤ n ≤ 3000
Example
Input Output
4 AAABACA
AACA
BABC
ABDA
AACA
Page 13 of 23
-
Dynamic Programming Jul 06, 2025
Problem N. Money Sums
Time Limit 1000 ms
Mem Limit 524288 kB
You have n coins with certain values. Your task is to find all money sums you can create
using these coins.
Input
The first input line has an integer n: the number of coins.
The next line has n integers x1 , x2 , … , xn : the values of the coins.
Output
First print an integer k : the number of distinct money sums. After this, print all possible
sums in increasing order.
Constraints
1 ≤ n ≤ 100
1 ≤ xi ≤ 1000
Example
Input Output
4 9
4 2 5 2 2 4 5 6 7 8 9 11 13
Page 14 of 23
-
Dynamic Programming Jul 06, 2025
Problem O. Removal Game
Time Limit 1000 ms
Mem Limit 524288 kB
There is a list of n numbers and two players who move alternately. On each move, a player
removes either the first or last number from the list, and their score increases by that
number. Both players try to maximize their scores.
What is the maximum possible score for the first player when both players play
optimally?
Input
The first input line contains an integer n: the size of the list.
The next line has n integers x1 , x2 , … , xn : the contents of the list.
Output
Print the maximum possible score for the first player.
Constraints
1 ≤ n ≤ 5000
−109 ≤ xi ≤ 109
Example
Input Output
4 8
4 5 1 3
Page 15 of 23
-
Dynamic Programming Jul 06, 2025
Problem P. Two Sets II
Time Limit 1000 ms
Mem Limit 524288 kB
Your task is to count the number of ways numbers 1, 2, … , n can be divided into two sets
of equal sum.
For example, if n = 7, there are four solutions:
{1, 3, 4, 6} and {2, 5, 7}
{1, 2, 5, 6} and {3, 4, 7}
{1, 2, 4, 7} and {3, 5, 6}
{1, 6, 7} and {2, 3, 4, 5}
Input
The only input line contains an integer n.
Output
Print the answer modulo 109 + 7.
Constraints
1 ≤ n ≤ 500
Example
Input Output
7 4
Page 16 of 23
-
Dynamic Programming Jul 06, 2025
Problem Q. Mountain Range
Time Limit 1000 ms
Mem Limit 524288 kB
There are n mountains in a row, each with a specific height. You begin your hang gliding
route from some mountain.
You can glide from mountain a to mountain b if mountain a is taller than mountain b and
all mountains between a and b.
What is the maximum number of mountains you can visit on your route?
Input
The first line has an integer n: the number of mountains.
The next line has n integers h1 , h2 , … , hn : the heights of the mountains.
Output:
Print one integer: the maximum number of mountains.
Constraints
1 ≤ n ≤ 2 ⋅ 105
1 ≤ hi ≤ 109
Example
Input Output
10 5
20 15 17 35 25 40 12 19 13 12
Page 17 of 23
-
Dynamic Programming Jul 06, 2025
Problem R. Increasing Subsequence
Time Limit 1000 ms
Mem Limit 524288 kB
You are given an array containing n integers. Your task is to determine the longest
increasing subsequence in the array, i.e., the longest subsequence where every element is
larger than the previous one.
A subsequence is a sequence that can be derived from the array by deleting some elements
without changing the order of the remaining elements.
Input
The first line contains an integer n: the size of the array.
After this there are n integers x1 , x2 , … , xn : the contents of the array.
Output
Print the length of the longest increasing subsequence.
Constraints
1 ≤ n ≤ 2 ⋅ 105
1 ≤ xi ≤ 109
Example
Input Output
8 4
7 3 5 3 6 2 9 8
Page 18 of 23
-
Dynamic Programming Jul 06, 2025
Problem S. Projects
Time Limit 1000 ms
Mem Limit 524288 kB
There are n projects you can attend. For each project, you know its starting and ending
days and the amount of money you would get as reward. You can only attend one project
during a day.
What is the maximum amount of money you can earn?
Input
The first input line contains an integer n: the number of projects.
After this, there are n lines. Each such line has three integers ai , bi , and pi : the starting
day, the ending day, and the reward.
Output
Print one integer: the maximum amount of money you can earn.
Constraints
1 ≤ n ≤ 2 ⋅ 105
1 ≤ ai ≤ bi ≤ 109
1 ≤ pi ≤ 109
Example
Input Output
4 7
2 4 4
3 6 6
6 8 2
5 7 3
Page 19 of 23
-
Dynamic Programming Jul 06, 2025
Problem T. Elevator Rides
Time Limit 1000 ms
Mem Limit 524288 kB
There are n people who want to get to the top of a building which has only one elevator.
You know the weight of each person and the maximum allowed weight in the elevator.
What is the minimum number of elevator rides?
Input
The first input line has two integers n and x: the number of people and the maximum
allowed weight in the elevator.
The second line has n integers w1 , w2 , … , wn : the weight of each person.
Output
Print one integer: the minimum number of rides.
Constraints
1 ≤ n ≤ 20
1 ≤ x ≤ 109
1 ≤ wi ≤ x
Example
Input Output
4 10 2
4 8 6 1
Page 20 of 23
-
Dynamic Programming Jul 06, 2025
Problem U. Counting Tilings
Time Limit 1000 ms
Mem Limit 524288 kB
Your task is to count the number of ways you can fill an n × m grid using 1 × 2 and 2 × 1
tiles.
Input
The only input line has two integers n and m.
Output
Print one integer: the number of ways modulo 109 + 7.
Constraints
1 ≤ n ≤ 10
1 ≤ m ≤ 1000
Example
Input Output
4 7 781
Page 21 of 23
-
Dynamic Programming Jul 06, 2025
Problem V. Counting Numbers
Time Limit 1000 ms
Mem Limit 524288 kB
Your task is to count the number of integers between a and b where no two adjacent digits
are the same.
Input
The only input line has two integers a and b.
Output
Print one integer: the answer to the problem.
Constraints
0 ≤ a ≤ b ≤ 1018
Example
Input Output
123 321 171
Page 22 of 23
-
Dynamic Programming Jul 06, 2025
Problem W. Increasing Subsequence II
Time Limit 1000 ms
Mem Limit 524288 kB
Given an array of n integers, your task is to calculate the number of increasing
subsequences it contains. If two subsequences have the same values but in different
positions in the array, they are counted separately.
Input
The first input line has an integer n: the size of the array.
The second line has n integers x1 , x2 , … , xn : the contents of the array.
Output
Print one integer: the number of increasing subsequences modulo 109 + 7.
Constraints
1 ≤ n ≤ 2 ⋅ 105
1 ≤ xi ≤ 109
Example
Input Output
3 5
2 1 3
Explanation: The increasing subsequences are [2], [1], [3], [2, 3] and [1, 3].
Page 23 of 23
-