Cactus Revenge
NE(E)RC featured a number of problems in previous years about cactuses — connected undirected graphs in which every edge belongs to at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. The traditional cactus that was initially used in NEERC 2005 problem is given on the second picture in the Examples section.
You are given n integers d[1]
, d[2]
, ..., d[n]
. Construct any cactus with n vertices such that vertex i has degree d[i]
(i. e. exactly d[i]
incident edges), or determine that no such cactus exists. Parallel edges and loops are not allowed.
Input
The first line contains a single integer n (2 ≤ n ≤ 2000) - the number of vertices in the cactus. The second line contains n integers d[1]
, d[2]
, ..., d[n]
(1 ≤ d[i]
≤ n - 1) - the desired vertex degrees.
Output
If it’s impossible to construct a cactus satisfying the conditions, output a single integer -1.
Otherwise, by tradition, output the constructed cactus as a set of edge-distinct paths.
In the first line print an integer m - the number of such paths. Each of the following m lines should contain a path in the graph. A path should start with an integer k[i]
(k[i]
≥ 2) followed by k[i]
integers from 1 to n. These k[i]
integers should represent consecutive vertices of this path. Adjacent vertices in the path should be distinct. The path can visit the same vertex multiple times, but every edge of the cactus should be traversed exactly once in the whole output.