Little CS is playing a game which involves a deck of cards numbered from to and an initially empty board. Each card has a positive integer value written on it, so that no two cards in the deck have the same value. The game is played as follows:
- Little CS reshuffles the cards so that they are in an order of his choice. Let , , , be the order of the cards in the deck after Little CS reshuffles them, where is the value written on the -th card;
- Little CS repeatedly picks the first card in the deck and has two options:
- If the card is dominant, he then has to place it on the board. A card is considered dominant if either the board is empty or the value written on it is at least twice as big as the value written on any card on the board.
- If the card is not dominant, he has to throw it away and cannot use it again.
- When the deck becomes empty, let be the sum of the values written on the cards on the board. Then, is the score of the game.
Task
Little CS is very competitive by nature, and desires that his score is as great as possible. In fact, he wonders what is the greatest score he could achieve. That is, what is the highest value of that could be obtained by choosing , , , Optimally. Little CS also wonders what is the number of orders , , , that lead to the highest value of .
This is too easy of a task for Little CS, so instead, he decided to make it harder and give it to you. Therefore, he has chosen cards with values written on them, and pairs of integers so that , and for each one of them, he asks you to calculate the two values he wishes to find, if he would only play the game with the cards .
Input data
The first line of the input contains , the number of cards, and , the number of queries you are expected to answer. The second line contains , , , , the values written on Little CS' cards. Each of the following lines contains two integers, the -th line containing the -th pair that Little CS has chosen for you.
Output data
Each of the lines of output must contain two space separated integers, representing the answer to Little CS' queries. More specifically, the -th line must contain the following values in the given order:
- The maximum value of that could be obtained by playing only with the cards numbered from to ;
- The number of arrays , , , that lead to the maximum score. Since this number could be quite large, you should output it modulo .
Implementation details
You may use the following function while implementing your solution:
int product(int a, int b);
This will return, in constant time, the value mod , i.e. the product of all integers between and modulo . Note that the parameters and need to satisfy .
Remember to include the header cards.h
, using the command #include "cards.h"
! Note that you should still implement the main
function.
Constraints and clarifications
- ;
- ;
- , for all , the elements of are pairwise distinct;
- , for all
- of the points for each subtask are awarded if the maximum value of is correct for all queries, and the other are awarded if the number of arrays is correct for all queries. Please note that you still have to output the number of arrays for each query, even if they are not correct, in order to get the points for finding the values of .
# | Points | Restrictions |
---|---|---|
1 | 8 | , for all |
2 | 15 | |
3 | 9 | |
4 | 10 | |
5 | 19 | |
6 | 21 | |
7 | 18 | No further restrictions. |
Example 1
stdin
6 2
4 1 7 12 5 21
1 4
4 6
stdout
17 2
26 1
Explanation
For each query, the numbers written on the cards that end up on the board are boxed.
For the first query, the following arrays lead to the maximal value of :
For the second query, the following arrays lead to the maximal value of :
Example 2
stdin
5 4
1 3 7 9 15
1 4
2 5
4 5
3 4
stdout
13 1
25 2
15 1
9 1
Example 3
stdin
10 6
115 4 319 19 28 1032 516 140 9 710
1 3
2 7
4 10
5 7
1 9
6 10
stdout
438 1
1580 8
1725 10
1576 1
1729 48
1697 2