{"problem":{"name":"J. Cola","description":{"content":"At Cola factory there is N bottles on a line. Also, there is a machine that pour cola in these bottles one by one. Every thing was working well. Suddenly, the machine started acting in a strange way !","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":4000,"memory_limit":65536},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10106J"},"statements":[{"statement_type":"Markdown","content":"At Cola factory there is N bottles on a line. Also, there is a machine that pour cola in these bottles one by one. Every thing was working well. Suddenly, the machine started acting in a strange way !!\n\nIt started choosing a random bottle and pour a random quantity of cola in this bottle.\n\nWhen a bottle is full, the rest of the cola goes to the bottle on the right of it, and if it was the last bottle, then the rest of the cola will be wasted . \n\nAs an engineer in this factory, you were given the capacity of every bottle in Litters. Also, you know the record of machine moves because it is stored in the Microcontroller memory of the machine. You are asked to compute the amount of wasted cola, and the amount of cola every bottle has at the end.\n\nYour program will be tested on one or more test cases. The first line of the input will be a single integer T the number of test cases. Followed by the test cases.\n\nEvery test case starts with two numbers N and M (1 ≤ N ≤ 105), (1 ≤ M ≤ 105) denoting the number of bottles, and the number of moves the machine had did.\n\nThen follow N integers on a line separated by spaces. The integer Ci denotes the capacity of the ith bottle (1 ≤ Ci ≤ 109) where (1 ≤ i ≤ N).\n\nM lines follow denoting the moves. Each line contains two integers X and Y (1 ≤ X ≤ N), (1 ≤ Y ≤ 109) means that the machine poured Y liters of cola in the Xth bottle . \n\nFor each test case print two lines.\n\nFirst line contains the amount of wasted cola.\n\nThe second line contains N integers separated by spaces denoting the amount of cola in every bottle at the end. \n\n## Input\n\nYour program will be tested on one or more test cases. The first line of the input will be a single integer T the number of test cases. Followed by the test cases.Every test case starts with two numbers N and M (1 ≤ N ≤ 105), (1 ≤ M ≤ 105) denoting the number of bottles, and the number of moves the machine had did.Then follow N integers on a line separated by spaces. The integer Ci denotes the capacity of the ith bottle (1 ≤ Ci ≤ 109) where (1 ≤ i ≤ N).M lines follow denoting the moves. Each line contains two integers X and Y (1 ≤ X ≤ N), (1 ≤ Y ≤ 109) means that the machine poured Y liters of cola in the Xth bottle . \n\n## Output\n\nFor each test case print two lines.First line contains the amount of wasted cola.The second line contains N integers separated by spaces denoting the amount of cola in every bottle at the end. \n\n[samples]","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $:  \n- Let $ K_k \\in \\mathbb{Z}_{\\geq 0} $ be the number of allowed rerolls.  \n- Let $ D_k = (d_1, d_2, \\dots, d_6) $ be a multiset of dice faces, where each $ d_i \\in \\{1, 2, 3, A, V, E\\} $.  \n\nLet $ F: \\mathcal{P}(\\{1,2,3\\})^6 \\to \\mathbb{R} $ be the scoring function defined as:  \nFor a multiset $ S \\subseteq \\{1,2,3\\}^6 $, let $ c_x $ be the count of face $ x \\in \\{1,2,3\\} $ in $ S $.  \nThen:  \n$$\nF(S) = \\sum_{x \\in \\{1,2,3\\}} \\max(0, c_x - 2) \\cdot x + \\min(c_x, 3) \\cdot x \\cdot \\mathbb{I}_{c_x \\geq 3}\n$$  \nWait — correction per example:  \n\n**Correct scoring rule**:  \nFor each face value $ x \\in \\{1,2,3\\} $, if it appears $ c_x $ times:  \n- If $ c_x \\geq 3 $, score $ x + (c_x - 3) $  \n- Else, score 0  \n\nThus:  \n$$\nF(S) = \\sum_{x \\in \\{1,2,3\\}} \\begin{cases} \nx + (c_x - 3) & \\text{if } c_x \\geq 3 \\\\\n0 & \\text{otherwise}\n\\end{cases}\n$$\n\n**Constraints**  \n1. $ 1 \\leq T \\leq \\text{unknown} $ (but input size manageable)  \n2. $ 0 \\leq K_k \\leq 2 $ (typical in game; implied by context)  \n3. Each die face $ d_i \\in \\{1,2,3,A,V,E\\} $; only $ \\{1,2,3\\} $ contribute to score.  \n\n**Objective**  \nFor each test case $ k $:  \n- Compute the **maximum expected score** $ \\mathbb{E}^* $ over all possible reroll strategies using at most $ K_k $ rerolls.  \n- A strategy: choose a subset $ R \\subseteq \\{1,2,\\dots,6\\} $ of dice to reroll (based on current face values), then reroll them up to $ K_k $ times (each reroll replaces selected dice with independent uniform random faces from $ \\{1,2,3,A,V,E\\} $).  \n- After all rerolls, compute final score $ F(\\text{final dice}) $.  \n- Find the **first optimal initial subset** $ R^* \\subseteq \\{d_1, \\dots, d_6\\} $ (by lexicographic order of face values, or as they appear) to reroll that achieves $ \\mathbb{E}^* $.  \n- Output:  \n  - First line: $ \\mathbb{E}^* $ (with error $ \\leq 10^{-4} $)  \n  - Second line: the multiset of face values of $ R^* $, in any order; if $ R^* = \\emptyset $ or $ K_k = 0 $, output \"-\"\n\n**Note**: Dice faces $ A, V, E $ are irrelevant to scoring and are rerolled as uniformly random among all 6 faces.  \n\n**Formal Objective**:  \n$$\n\\mathbb{E}^* = \\max_{R \\subseteq D_k} \\mathbb{E}[F(\\text{result after } K_k \\text{ rerolls of } R) \\mid \\text{initial dice } D_k]\n$$  \nFind the lexicographically first (or any) subset $ R^* $ achieving this maximum.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10106J","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}