Arranging Books
InterviewTime limit2sMemory limit1024 MB
Count the orderings of n books on a shelf where every adjacent pair's combined page count is at most the limit a_i between them.
- Level
Easy3 of 10
- Topics
- Brute force, Backtracking
- Solved
- No attempts yet
Problem
At the start of the school year, the library of a large university lent books to its students so they could study over the year. Olechka, a student, waited in a huge line, received her books, and carried them home with great effort. Now she wants to place these scientific works on her bookshelf.
Unfortunately, Olechka is not happy with every arrangement. She is upset by any two neighboring books on the shelf whose combined page count is more than a given value.
More precisely, Olechka becomes upset if there is a pair of books in positions and of the shelf whose total number of pages is greater than .
Help poor Olechka find how many ways there are to arrange the books on the shelf so that she is not upset.
Input
The first line contains the number (), the number of Olechka's books. The second line contains integers (), the number of pages in each book. The third line contains integers (), the allowed total number of pages in the books at positions and of the shelf.
Output
Print the number of ways to arrange the books on the shelf.