Mansur loves to breed horses, just like his ancient ancestors did. He now has the largest herd in Kazakhstan. But this was not always the case.
Let us number the years from
Horses could only be sold at the end of a year. For each year
Mansur wonders what is the largest amount of money he could have now if he had chosen the best moments to sell his horses during the
Mansur's memory improves throughout the evening, and so he makes a sequence of
The actual answers to Mansur's questions can be huge. In order to avoid working with large numbers, you are only required to report the answers modulo
Example
Suppose that there are
0 | 1 | 2 | |
---|---|---|---|
X | 2 | 1 | 3 |
Y | 3 | 4 | 1 |
For these initial values, Mansur can earn the most if he sells both his horses at the end of year 1. The entire process will look as follows:
- Initially, Mansur has 1 horse.
- After year 0 he will have
horses. - After year 1 he will have
horses. - He can now sell those two horses. The total profit will be
.
Then, suppose that there is
After the update we will have:
0 | 1 | 2 | |
---|---|---|---|
X | 2 | 1 | 3 |
Y | 3 | 2 | 1 |
In this case, one of the optimal solutions is to sell one horse after year 0 and then three horses after year 2. The entire process will look as follows:
- Initially, Mansur has 1 horse.
- After year 0 he will have
horses. - He can now sell one of those horses for
, and have one horse left. - After year 1 he will have
horse. - After year 2 he will have
horses. - He can now sell those three horses for
. The total amount of money is .
Task
You are given init
, updateX
, and updateY
.
int init(int N, int X[], int Y[])
- The grader will call this function first and exactly once.
N
: the number of years.X
: an array of length . For , gives the growth coefficient for year .Y
: an array of length . For , gives the price of a horse after year .- Note that both
X
andY
specify the initial values given by Mansur (before any updates). - After init terminates, the arrays
X
andY
remain valid, and you may modify their contents if you wish. - The function should return the maximal amount of money Mansur could get for these initial values of
and , modulo .
int updateX(int pos, int val)
pos
: an integer from the range .val
: the new value for .- The function should return the maximal amount of money Mansur could get after this update, modulo
.
int updateY(int pos, int val)
pos
: an integer from the range .val
: the new value for .- The function should return the maximal amount of money Mansur could get after this update, modulo
.
You may assume that all the initial, as well as updated values of
After calling init
, the grader will call updateX
and updateY
an integer number of times. The total number of calls to updateX
and updateY
will be
Subtasks
subtask | points | additional constraints | ||
---|---|---|---|---|
1 | 17 | |||
2 | 17 | none | ||
3 | 20 | init and updateX respectively | ||
4 | 23 | none | ||
5 | 23 | none |
Comments