#P8365. [LNOI2022] 吃

[LNOI2022] 吃

Problem Description

Little A really likes eating.

There are nn portions of food in front of Little A. The ii-th portion has parameters aia_i and bib_i. Little A can eat these nn portions in any order. When she eats the food numbered ii, she may choose to multiply her weight by aia_i, or add bib_i to her weight. Each portion of food must be eaten exactly once.

Little A's initial weight is 11. Please find the maximum weight she can reach after eating all nn portions of food. The answer may be very large; you only need to output it modulo (109+7)({10}^9 + 7).

Note: You need to maximize the weight first, and then take the maximum value modulo (109+7)\bm{({10}^9 + 7)}, rather than maximizing the value of (weight modulo (109+7)\bm{({10}^9 + 7)}).

Input Format

The first line contains an integer nn, the number of portions of food. The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, and the third line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n, describing the parameters of each portion.

Output Format

Output one integer, the maximum weight Little A can obtain modulo (109+7)({10}^9 + 7).

5
1 2 3 4 5
100 200 300 400 500

18060

Hint

[Sample Explanation #1]

The following plan can achieve the maximum weight:

  • Eat the first portion and choose to increase the weight by 100100, making the weight 101101.
  • Eat the second portion and choose to increase the weight by 200200, making the weight 301301.
  • Eat the third portion and choose to multiply the weight by 33, making the weight 903903.
  • Eat the fourth portion and choose to multiply the weight by 44, making the weight 36123612.
  • Eat the fifth portion and choose to multiply the weight by 55, making the weight 1806018060.

[Sample #2]

See food/food2.in and food/food2.ans in the attachment.

This sample satisfies n10n \le 10 and special property E.

[Sample #3]

See food/food3.in and food/food3.ans in the attachment.

This sample satisfies n20n \le 20 and special property E.

[Sample #4]

See food/food4.in and food/food4.ans in the attachment.

This sample satisfies n2000n \le 2000.

[Sample #5]

See food/food5.in and food/food5.ans in the attachment.

This sample satisfies special property A.

[Sample #6]

See food/food6.in and food/food6.ans in the attachment.

This sample satisfies special property C.

[Sample #7]

See food/food7.in and food/food7.ans in the attachment.

This sample satisfies special property D.

[Sample #8]

See food/food8.in and food/food8.ans in the attachment.

This sample satisfies special property B.

[Sample #9]

See food/food9.in and food/food9.ans in the attachment.

[Constraints]

For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times {10}^5, 1ai,bi1061 \le a_i, b_i \le {10}^6.

Test Point ID nn \le Special Properties
11 1010 DE
22 E
33 AE
44 E
55 2020 DE
66 E
77
88
99 20002000 D
1010 None
1111
1212
1313 5×1055 \times {10}^5 BD
1414 B
1515 C
1616
1717 105{10}^5 None
1818
1919 5×1055 \times {10}^5
2020

Special property A: ai=1a_i = 1.
Special property B: aibia_i \ge b_i.
Special property C: ai,bia_i, b_i are generated independently and uniformly at random within [1,106][1, {10}^6].
Special property D: ai2a_i \ge 2.
Special property E: ai4a_i \le 4.

Translated by ChatGPT 5