#P2125. 图书馆书架上的书

图书馆书架上的书

Description

There are nn bookshelves in a circle. The shelf behind shelf 11 is shelf 22, the shelf behind shelf 22 is shelf 33, …, the shelf behind shelf n1n-1 is shelf nn, and the shelf behind shelf nn is shelf 11. Shelf ii has bib_i books. To make the library look nicer, WZF shenniu asks SY to move books so that every shelf ends up with the same number of books. Because the total number of moved books might be large, SY is only allowed to move books from a shelf to its two adjacent shelves. What is the minimum number of books SY needs to move?

Input Format

There are 22 lines. The first line contains a positive integer nn. The second line contains nn non-negative integers, where the ii-th is bib_i.

Output Format

Output n+1n+1 lines.

The first line contains a positive integer mm, the minimum total number of books SY needs to move.

Then output nn lines. In the ii-th line, output two integers afiaf_i and abiab_i, meaning SY moves afiaf_i books and abiab_i books from shelf ii to the shelf in front of it and the shelf behind it, respectively.

5
15 7 11 3 14

12
2 3
-3 0
0 1
-1 -6
6 -2

Hint

Constraints and Notes

For all testdata, 1n105+11 \le n \le 10^5 + 1, and nn is odd; bi107b_i \le 10^7.

If afiaf_i is negative, it means SY moves afi-af_i books from the shelf in front of shelf ii onto shelf ii.

Similarly, if abiab_i is negative, it means SY moves abi-ab_i books from the shelf behind shelf ii onto shelf ii.

Translated by ChatGPT 5