#P11044. Maximizing Dining Capacity

    ID: 13098 Type: Default 1000ms 256MiB

Maximizing Dining Capacity

Maximizing Dining Capacity

In School S there are a2 two-bed dorms, a3 three-bed dorms, and a4 four-bed dorms. The cafeteria has b4 four-seat tables and b6 six-seat tables. The school wants to arrange meals such that all students from any given dorm room sit together at the same table. Note that a table can seat one or more dorm groups as long as the total number of students does not exceed the table's capacity.

For a four-seat table (capacity = 4), the possible assignments are:

  • (4): a single four-bed dorm (4 students) fills the table exactly.
  • (2,2): two two-bed dorms (2+2 = 4).
  • (3): one three-bed dorm (3 students, with 1 seat wasted).
  • (2): one two-bed dorm (2 students, with 2 seats wasted).

For a six-seat table (capacity = 6), the possible assignments (in order of preference to maximize the number of students seated) are:

  • (4,2): one four-bed dorm and one two-bed dorm (4+2 = 6).
  • (3,3): two three-bed dorms (3+3 = 6).
  • (2,2,2): three two-bed dorms (2+2+2 = 6).
  • (3,2): one three-bed and one two-bed dorm (3+2 = 5).
  • (4): a single four-bed dorm (4 students).
  • (3): a single three-bed dorm (3 students).
  • (2): a single two-bed dorm (2 students).

Given the available dorms and tables, determine the maximum number of students that can be arranged to dine simultaneously.

The assignment strategy is as follows: first assign dorm groups to the six-seat tables (because they offer more flexibility and allow for optimal combinations to achieve 6 seats per table), then assign the remaining dorm groups to the four-seat tables. You must choose assignments in each table that do not exceed its capacity and maximize the sum of students seated.

All formulas are given in LaTeX format. For example, the number of two-bed dorms is denoted by $a_2$, three-bed dorms by $a_3$, four-bed dorms by $a_4$, four-seat tables by $b_4$, and six-seat tables by $b_6$.

inputFormat

The input consists of a single line containing five space-separated integers:

a2 a3 a4 b4 b6

Here,

  • $a_2$ is the number of two-bed dorms,
  • $a_3$ is the number of three-bed dorms,
  • $a_4$ is the number of four-bed dorms,
  • $b_4$ is the number of four-seat tables,
  • $b_6$ is the number of six-seat tables.

outputFormat

Output a single integer representing the maximum number of students that can dine simultaneously according to the rules.

sample

1 1 1 1 1
9