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

#GD0000194. Đưa mọi người qua cầu (Bridge)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đưa mọi người qua cầu (Bridge)

Bridge

Source: UVa

Version: Phuoc Hung OJ (single-case adaptation where applicable)

Problem Statement

There are nn people who must cross a bridge at night. Person ii needs tit_i seconds to cross. There is only one flashlight, and each crossing carries one or two people and the flashlight. Two people cross at the slower person’s time. The flashlight must be returned while people remain on the starting side. Find the minimum total time and output a sequence of crossings attaining it. People with equal crossing times are distinct, but are represented by their times, as in UVa.

Input

The first line contains nn. Exactly nn positive times tit_i follow, possibly one per line. There is no number of test cases and no blank-line separator.

Output

The first line prints the minimum time. Each following line lists one or two crossing times representing the people who travel with the flashlight from its current side. Directions alternate, starting from the original bank, and everyone must end on the far bank. Any optimal valid schedule is accepted.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ti≤201\le t_i\le 20 for every 1≤i≤n1\le i\le n; repeated times are allowed.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le 150; 1≤ti≤1001\le t_i\le 100 for every 1≤i≤n1\le i\le n; repeated times are allowed.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤ti≤1001\le t_i\le 100 for every 1≤i≤n1\le i\le n; repeated times are allowed.

Examples

Example 1

Input

4
1
2
5
10

Output

17
1 2
1
5 10
2
1 2

Explanation

Send 1 and 2 across (2), return 1 (1), send 5 and 10 across (10), return 2 (2), and send 1 and 2 across (2). Total: 17.

Example 2

Input

4
1
1
2
2

Output

6
1 1
1
2 2
1
1 1

Explanation

Send both 1-time people across (1), one returns (1), send both 2-time people across (2), a 1-time person returns (1), then both 1-time people cross (1), totaling 6.