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

#GD0000052. Hai dãy tam phân (Ternary Sequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Hai dãy tam phân (Ternary Sequence)

Ternary Sequence

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Two equal-length sequences contain only 0, 1, 2, with frequency triples (x1,y1,z1)(x_1,y_1,z_1) and (x2,y2,z2)(x_2,y_2,z_2). Rearrange both freely. Define ci=aibic_i=a_ib_i if ai>bia_i>b_i, 00 if equal, and −aibi-a_ib_i if ai<bia_i<b_i. Maximize ∑ci\sum c_i.

Input

Line 1: x1,y1,z1x_1,y_1,z_1. Line 2: x2,y2,z2x_2,y_2,z_2.

Output

Print the maximum sum.

Subtasks

  • Subtask 1 (20 points): 0≤x1,y1,z1,x2,y2,z2≤50\le x_1,y_1,z_1,x_2,y_2,z_2\le 5; 1≤x1+y1+z1=x2+y2+z21\le x_1+y_1+z_1=x_2+y_2+z_2.
  • Subtask 2 (30 points): 0≤x1,y1,z1,x2,y2,z2≤100000\le x_1,y_1,z_1,x_2,y_2,z_2\le 10000; 1≤x1+y1+z1=x2+y2+z21\le x_1+y_1+z_1=x_2+y_2+z_2.
  • Subtask 3 (50 points): 0≤x1,y1,z1,x2,y2,z2≤1000000000\le x_1,y_1,z_1,x_2,y_2,z_2\le 100000000; 1≤x1+y1+z1=x2+y2+z21\le x_1+y_1+z_1=x_2+y_2+z_2.

Examples

Input

2 3 2
3 3 1

Output

4

Explanation

Two 2s in aa can be matched with two 1s in bb, each contributing +2. Remaining elements can be paired without negative contributions, giving the maximum 4.