CCCGuide

Problem S1: Positioning Peter's Paintings

2025 · S1 · Original problem · DMOJ

Problem Description

Peter the painter just finished painting two rectangular paintings and would like to display both on a rectangular wall which has the smallest perimeter possible. The first painting has a base of length AA units and a height of length BB units. The second painting has a base of length XX units and a height of length YY units.

Peter has a few conditions on how to arrange his paintings on the rectangular wall. The first condition is that the paintings must be upright, meaning that the bases of the paintings are parallel to the floor. The second condition is that he would like to display both paintings in full, meaning that they cannot overlap each other. Please help determine the rectangular wall of minimum perimeter such that the paintings can be displayed without violating his conditions.

Input Specification

The one line of input will consist of four space-separated positive integers, AA, BB, XX, YY (1≤A,B,X,Y≤108)(1 \le A, B, X, Y \le 10^8).

The following table shows how the available 1515 marks are distributed:

MarksBrief Description
55Paintings are congruent squares
55Paintings are squares
55Paintings are rectangles (possibly squares)

Output Specification

Output a single integer representing the minimum perimeter of a rectangular wall without violating Peter's conditions.

Sample Input 1

3 3 3 3

Output for Sample Input 1

18

Explanation of Output for Sample Input 1

This test case satisfies all subtasks. An optimal arrangement using a 6-by-3 wall is shown below.

Square Painting 1, with width 3 and height 3, is placed beside square Painting 2, with width 3 and height 3, forming a larger rectangle with width 6 and height 3.

Sample Input 2

2 2 4 4

Output for Sample Input 2

20

Explanation of Output for Sample Input 2

This test case satisfies the second and third subtasks. An optimal arrangement using a 6-by-4 wall is shown below.

Square Painting 1, with width 2 and height 2, is placed beside square Painting 2, with width 4 and height 4, so that the top edge of Painting 1 is 1 unit below the top edge of Painting 2. A rectangle with width 6 and height 4 encloses the two paintings.

Sample Input 3

1 2 3 1

Output for Sample Input 3

12

Explanation of Output for Sample Input 3

This test case satisfies the last subtask. An optimal arrangement using a 3-by-3 wall is shown below.

Rectangular Painting 1 with width 1 and height 2, is placed below rectangular Painting 2, with width 3 and height 1, so that the left edge of Painting 1 is 0.5 units to the right of the left edge of Painting 2. A square with width 3 and height 3 encloses the two paintings.