Little Shino and Contests

2.4

13 votes
Algorithms, Approved, Dynamic Programming, Easy
Problem

Given two array A and B of length 5. You have 5 problems to solve in a contest. For ith problem you need exactly Ai minutes to solve. But you can't concentrate on ith problem for more than Bi minutes continuously. Find number of ways to schedule the i=15Ai minutes so that you will solve all the 5 problems (mod 109+7)? Scheduling means for each minute, you have to decide on which problem you will concentrate during that minute.

Two schedules are considered different if at-least for one of the i=15Ai minutes, you are concentrating on different problems in both the schedules.

Input Format:
First line contains 5 space separated integers, denoting Ai (1Ai10). Second line contains 5 space separated integers, denoting Bi (1Bi10).

Output Format:
Find number of ways to schedule the i=15Ai minutes so that you will solve all the 5 problems (mod 109+7)

Sample Input
1 1 1 1 1
1 1 1 1 1
Sample Output
120
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first minute, we have 5 problem to select from.
For the second minute, we have 4 problem to select from as we have solved one of the problem in first minute.
For the third minute, we have 3 problem to select from as we have solved two of the problem in first two minutes.
For the fourth minute, we have 2 problem to select from as we have solved three of the problems in first three minutes.
For the fourth minute, we have only one problem left.
So total number of ways = 5!=120.

Editor Image

?