PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000271. Lập lịch công việc ngắn (Small Schedule)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lập lịch công việc ngắn (Small Schedule)

Small Schedule

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

There are MM identical idle machines, SS jobs of duration one second and LL jobs of duration QQ seconds. A job must run entirely on one machine, which processes at most one job at a time. Assign and order the jobs freely to minimize the completion time of all jobs.

Input

One line contains four integers Q,M,S,LQ,M,S,L, in that order.

Output

Print the minimum completion time in seconds.

Subtasks

  • Subtask 1 (20 points): 2≤Q≤102\le Q\le10; 1≤M≤41\le M\le4; 0≤S,L≤60\le S,L\le6.
  • Subtask 2 (30 points): 2≤Q≤10002\le Q\le1000; 1≤M≤1061\le M\le10^6; 0≤S≤1060\le S\le10^6; L=0L=0.
  • Subtask 3 (50 points): 2≤Q≤10002\le Q\le1000; 1≤M≤1061\le M\le10^6; 0≤S,L≤1060\le S,L\le10^6.

Examples

Input 1

2 4 3 6

Output 1

4

Explanation

Assign long jobs in counts (2,2,1,1), leaving four seconds on the last two machines for short jobs. Below four seconds each machine can handle only one long job.