You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n_1 washing machines, n_2 drying machines and n_3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.

It takes t_1 minutes to wash one piece of laundry in a washing machine, t_2 minutes to dry it in a drying machine, and t_3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have.


-----Input-----

The only line of the input contains seven integers: k, n_1, n_2, n_3, t_1, t_2, t_3 (1 ≤ k ≤ 10^4; 1 ≤ n_1, n_2, n_3, t_1, t_2, t_3 ≤ 1000).


-----Output-----

Print one integer — smallest number of minutes to do all your laundry.


-----Examples-----
Input
1 1 1 1 5 5 5

Output
15

Input
8 4 3 2 10 5 2

Output
32



-----Note-----

In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.

In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes.

Difficulty: HARD
Tags: Implementation

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
(k, na, nb, nc, ta, tb, tc) = list(map(int, input().split()))
from collections import deque
a = deque()
b = deque()
c = deque()
for i in range(na):
	a.append(0)
for i in range(nb):
	b.append(0)
for i in range(nc):
	c.append(0)
t = 0
for i in range(k):
	vr = max(a[0], b[0] - ta, c[0] - (ta + tb))
	a.popleft()
	a.append(vr + ta)
	b.popleft()
	b.append(vr + tb + ta)
	c.popleft()
	c.append(vr + ta + tb + tc)
	t = vr + ta + tb + tc
print(t)


class step(object):
	timer = []

	def __init__(self, name, machin_num, step_time):
		self.machin_num = machin_num
		self.step_time = step_time
		self.name = name

	def step_input(self, cloth_num, t):
		for new_cloth in range(cloth_num):
			self.timer.append(t)

	def step_run(self, t):
		tmptimer = [each_timer for each_timer in self.timer if t - each_timer < self.step_time]
		self.timer = tmptimer

	def checkstate(self, pre_t):
		running_machine = len(self.timer)
		for each_timer in range(running_machine):
			if pre_t - self.timer[each_timer] >= self.step_time:
				running_machine -= 1
		return self.machin_num - running_machine

def main():
	(p, n1, n2, n3, t1, t2, t3) = list(map(int, input().split()))
	step1 = step('step1', n1, t1)
	step2 = step('step2', n2, t2)
	step3 = step('step3', n3, t3)
	t = 0
	while True:
		pre_num1 = step1.checkstate(t)
		pre_num2 = step2.checkstate(t + t1)
		pre_num3 = step3.checkstate(t + t1 + t2)
		step1_input = min(pre_num1, pre_num2, pre_num3)
		p -= step1_input
		step1.step_run(t)
		step1.step_input(step1_input, t)
		step2.step_run(t + t1)
		step2.step_input(step1_input, t + t1)
		step3.step_run(t + t1 + t2)
		step3.step_input(step1_input, t + t1 + t2)
		if p <= 0:
			print(t + t1 + t2 + t3)
			break
		pre_t = []
		if len(step1.timer) == step1.machin_num:
			pre_t.append(t1 - (t - step1.timer[0]))
		if len(step2.timer) == step2.machin_num:
			pre_t.append(t2 - (t + t1 - step2.timer[0]))
		if len(step3.timer) == step3.machin_num:
			pre_t.append(t3 - (t + t1 + t2 - step3.timer[0]))
		if len(pre_t) == 0:
			pre_t = 1
		else:
			pre_t = min(pre_t)
		t += pre_t

def __starting_point():
	main()
__starting_point()


(k, n1, n2, n3, t1, t2, t3) = map(int, input().split())
x1 = x2 = x3 = 0
(T1, T2, T3) = ([0] * n1, [0] * n2, [0] * n3)
for i in range(k):
	T1[x1] += t1
	T2[x2] = max(T1[x1], T2[x2]) + t2
	T3[x3] = max(T2[x2], T3[x3]) + t3
	x1 += 1
	x2 += 1
	x3 += 1
	if x1 == n1:
		x1 = 0
	if x2 == n2:
		x2 = 0
	if x3 == n3:
		x3 = 0
print(T3[x3 - 1])