problems / 160

Cooling Down the Task Runner

medium greedy heap queue simulation math 1000 ms · 256 MB

Cooling Down the Task Runner

A task runner works in numbered time slots 1, 2, 3, …. In every slot it either executes exactly one task or stays idle. Every task takes exactly one slot and has a type, an uppercase letter from A to Z.

After a task of some type runs, the runner has to cool down before it can run another task of the same type: if two tasks of the same type run in slots i < j, then at least n slots must lie strictly between them, that is j − i ≥ n + 1. Tasks of different types never restrict each other, and tasks may be executed in any order.

You are given how many tasks of each type must be executed. Find the smallest T such that all tasks can be executed in slots 1 … T. When the number of tasks is small, also print one schedule achieving it.

Input

The first line contains t (1 ≤ t ≤ 10^4), the number of test cases. Each test case consists of two lines:

  • the cooldown n (0 ≤ n ≤ 10^5);
  • 26 integers c_A, c_B, …, c_Z (0 ≤ c_i ≤ 10^6), the number of tasks of each type.

In every test case the total number of tasks m = c_A + … + c_Z is at least 1. The sum of m over all test cases does not exceed 10^6.

Output

For each test case print the minimum number of slots T on its own line.

If m ≤ 1000, then print m more lines describing one optimal schedule: each line contains a slot number s and a letter, meaning a task of that type runs in slot s. The lines must be ordered by strictly increasing s, every s must satisfy 1 ≤ s ≤ T, each type must appear exactly as many times as it has tasks, and the cooldown rule must hold. Any optimal schedule is accepted.

If m > 1000, print only T.

Notes

In the first example the first test case has n = 2 with three A and three B tasks. A schedule A B . A B . A B (dots are idle slots) uses 8 slots. Fewer is impossible: three tasks of one type span at least 7 slots (first to last), and the first A and the first B cannot share slot 1, so one of the two types finishes no earlier than slot 8. In the second test case there is no cooldown, so the runner is never idle.

The answer may not fit in a 32-bit integer.

Any correct answer is accepted.

Example 1

Input

2
2
3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Output

8
1 A
2 B
4 A
5 B
7 A
8 B
3
1 A
2 B
3 A

Example 2

Input

2
2
3 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
100000
2000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Output

8
1 A
2 B
3 C
4 A
5 B
6 C
7 A
8 D
199902000

Hints · reveal one at a time if you're stuck

Hint 1

Consider the most frequent task type. Its tasks must be spaced out, so how many slots does it force on its own, and what happens when several types share the top count?

Hint 2

Fill slots from left to right, and in each slot run an available task whose type still has the most tasks left. Argue that this keeps the heavy types from getting stuck at the end.

Hint 3

The answer can be around 10^11, so you cannot step slot by slot. Track types that are cooling down in a queue ordered by when they become available, and types that are ready in a heap by remaining count, jumping over idle stretches when nothing is ready.

report an issue Prefer your own editor? sooks prep 160 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.