問題一覧 > 通常問題

No.178 Beautiful Whitespace (1)

レベル : / AI推定難易度 : 151 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 64 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 492
作問者 : ぴろず
お気に入りにしたユーザー ProblemId : 424 / 自分の提出
問題文最終更新日: 2026-06-25 16:56:32
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 96 (順位表) の他の問題:

Problem Statement

Taro is absorbed in writing programs in the esoteric programming language Whitespace.
Whitespace uses only spaces, tabs, and newlines for its syntax and ignores everything else.

Taro is particular about the beauty of source code, and he calls a source code "perfect" when all of its lines have equal width.
Taro is trying to make the Whitespace source code he wrote "perfect" by inserting full-width spaces into it.
Write a program that finds the minimum number of full-width spaces Taro needs to make the source code "perfect".
Here, when a line has $a$ spaces, $b$ tabs, and $c$ full-width spaces, the width of that line is $(a + 4b + 2c)$.
Unlike in ordinary editors, the positions of tabs and the like do not affect the width.

Input

$N$
$a_1$ $b_1$
$\vdots$
$a_N$ $b_N$

Line $1$ gives $N$, the number of lines of the source code Taro wants to make perfect.
The following $N$ lines give information on each line of the source code. Line $i$ has $a_i$ spaces and $b_i$ tabs.

$2 \leq N \leq 1000$
$0 \leq a_i, b_i \leq 10^7$
Taro's source code contains no characters other than spaces, tabs, and newlines.
Taro's source code is not necessarily grammatically valid Whitespace.

Output

If Taro can make the Whitespace source code "perfect", output the minimum number of full-width spaces required.
If it cannot be made "perfect", output $-1$.
Print a newline at the end.

Sample

Sample 1
Input
2
1 1
3 0
Output
1

Line $1$ has $1$ space and $1$ tab, so its width is $1 + 1 \times 4 = 5$.
Line $2$ has $3$ spaces and $0$ tabs, so its width is $3 + 0 \times 4 = 3$.
As it stands, the code is not "perfect", but adding $1$ full-width space to line $2$ makes the width of every line $5$, which makes it "perfect", so output $1$.

Sample 2
Input
4
0 0
0 0
0 0
0 0
Output
0

The width of every line is $0$ from the start, so the code is already "perfect".
No full-width spaces need to be added, so output $0$.

Sample 3
Input
2
0 0
1 0
Output
-1

The width of line $1$ is $0$, and the width of line $2$ is $1$.
No matter how full-width spaces are added, the code cannot be made "perfect", so output $-1$.

Sample 4
Input
4
30 0
20 10
10 20
0 30
Output
90

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。