P26541 [AtCoder] Self Study
普及+
题目描述

Problem Description

The first year of JOI High School has N subjects that are taught over M weeks, from the 1st week to the Mth week. Each subject is numbered from 1 to N. There are N class periods each week, and the i-th period (1 ≤ i ≤ N) of each week is dedicated to the subject i.

Bitaro, a first-year student, can choose one of the following actions for each of the N × M class periods:

  • Action 1: Attend the class of that period. Attending the class of subject i (1 ≤ i ≤ N) increases the understanding of that subject by Ai.
  • Action 2: Skip the class and choose one subject to self-study. Self-studying subject i (1 ≤ i ≤ N) increases the understanding of that subject by Bi.

Initially, the understanding of all subjects is 0. Bitaro wants to focus on competitive programming practice after school, so he will not study outside of class hours.

At the end of the semester, a final exam will be held. Bitaro does not want to fail the exam, so he wants to maximize the minimum understanding among all subjects by the time of the exam.

Write a program that, given the length of the semester, the number of subjects, and the increments in understanding for attending classes and self-studying, calculates the maximum possible value of the minimum understanding among all subjects by the time of the exam.


Input Format

The input is given from the standard input in the following format. All values are integers.

N M A1 A2 ... AN B1 B2 ... BN

Output Format

Output the maximum possible value of the minimum understanding among all subjects by the time of the exam to the standard output in one line.


Sample Input and Output

Sample Input #1

3 3
19 4 5
2 6 2

Sample Output #1

18

Sample Input #2

2 1
9 7
2 6

Sample Output #2

7

Sample Input #3

5 60000
630510219 369411957 874325200 990002527 567203997
438920902 634940661 593780254 315929832 420627496

Sample Output #3

41397427274960

Sample Input #4

4 25
1 2 3 4
1 2 3 4

Sample Output #4

48

Notes/Hints

Constraints

  • 1 ≤ N ≤ 300,000.
  • 1 ≤ M ≤ 1,000,000,000.
  • 1 ≤ Ai ≤ 1,000,000,000 (1 ≤ i ≤ N).
  • 1 ≤ Bi ≤ 1,000,000,000 (1 ≤ i ≤ N).

Subtasks

  1. (10 points) M = 1.
  2. (25 points) N × M ≤ 300,000, Ai = Bi (1 ≤ i ≤ N).
  3. (27 points) N × M ≤ 300,000.
  4. (29 points) Ai = Bi (1 ≤ i ≤ N).
  5. (9 points) No additional constraints.

Sample Explanation 1

For example, studying in the following way results in the understandings of subjects 1, 2, and 3 being 19, 18, and 19, respectively, by the time of the exam.
- The 1st period of the 1st week: Self-study subject 2.
- The 2nd period of the 1st week: Self-study subject 2.
- The 3rd period of the 1st week: Attend the class of subject 3.
- The 1st period of the 2nd week: Attend the class of subject 1.
- The 2nd period of the 2nd week: Self-study subject 3.
- The 3rd period of the 2nd week: Attend the class of subject 3.
- The 1st period of the 3rd week: Self-study subject 3.
- The 2nd period of the 3rd week: Self-study subject 2.
- The 3rd period of the 3rd week: Attend the class of subject 3.

There is no way to make the minimum understanding 19 or more, so output 18. This input-output example satisfies the constraints of subtasks 3 and 5.


Sample Explanation 2

This input-output example satisfies the constraints of subtasks 1, 3, and 5.


Sample Explanation 3

This input-output example satisfies the constraints of subtasks 3 and 5.


Sample Explanation 4

This input-output example satisfies the constraints of subtasks 2, 3, 4, and 5.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 3 19 4 5 2 6 2
样例输出
18