Connecting Square Tiles
Time limit1sMemory limit1024 MB
Given N unit square tiles, find the minimum perimeter of one connected figure formed by placing them on a grid without overlap.
- Level
Medium4 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Given square tiles, all with the same shape and size, each with side length . There is a floor covered with a grid of unit squares. Join the tiles on this floor to form a single figure.
Tiles can only be placed on the cells of the grid, and tiles cannot overlap or be stacked. All tiles must be connected to each other.

Given the number of square tiles, find the minimum possible perimeter of a figure that can be formed by joining them according to the rules above. You must solve test cases in one input.
Input
The first line contains the number of test cases . ()
Each of the next lines contains the number of tiles for one test case. ()
The input data can be large, so keep this in mind.
Output
For each test case, in the order given, print the minimum possible perimeter on its own line.
Hint
This is a problem where the same task is solved many times within one input. To be accepted, the program must print correct answers for every test case in the input within the time limit, then exit.