luogu#P2125. 图书馆书架上的书

图书馆书架上的书

Background

With NOIP 2014 approaching, the JC Academy Informatics Club is actively preparing. So usqwedf, Liang Dada, Xumuban Zhuanchang, YH dashen, and LHT dashen also plan to launch the “Lanxiang Cup.”

In the library, MC and others successively hosted the JC Academy joint contests “Qiliaobei” and “UID#3.” It is said that YH dashen is also painstakingly studying network flow. Hearing this, WZF shenniu and MZC shenniu from JC Academy Grade 13 Class 13 decided to co-create the “Shisandian Cup.” But making a full problem set is hard work, so they decided to rope in SY, a fellow classmate and club member who was in the library searching for the “Hello World” standard solution.

Poor juruo SY had tons of homework and refused to agree. Finally, WZF shenniu compromised: “I’ll give you one problem. If you solve it, we won’t make you write problems; otherwise… you know.” SY had barely finished reading the problem WZF improvised when he said, almost in tears, “You win.”

But juruo SY was really too weak to write problems, so he racked his brains and came up with an idea—just copy the problem WZF gave.

Problem 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