{"problem":{"name":"Bought Review","description":{"content":"Solve the following problem for $T$ test cases. On the gourmet review site EatCocoder, you can review restaurants with an integer number of stars from $1$ to $5$.   Initially, the restaurant managed b","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc174_b"},"statements":[{"statement_type":"Markdown","content":"Solve the following problem for $T$ test cases.\nOn the gourmet review site EatCocoder, you can review restaurants with an integer number of stars from $1$ to $5$.  \nInitially, the restaurant managed by Chef B has $A_i$ reviews with $i$ stars. $(1 \\le i \\le 5)$  \nThe chef can pay a bribe of $P_i$ yen to the EatCocoder administration to have one additional $i$\\-star review. $(1 \\le i \\le 5)$\nAfter adding a total of $k$ reviews by bribery, there will be $A_1+A_2+A_3+A_4+A_5+k$ reviews in total.  \nChef B wants the average rating of these reviews to be at least $3$ stars. Determine the minimum total amount of bribery required to achieve this.\n\n## Constraints\n\n*   All input values are integers.\n*   $1 \\le T \\le 10^4$\n*   $0 \\le A_i \\le 10^8$\n*   $1 \\le A_1+A_2+A_3+A_4+A_5$\n*   $1 \\le P_i \\le 10^8$\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$T$\n$\\rm{Case}_1$\n$\\rm{Case}_2$\n$\\vdots$\n$\\rm{Case}$$_T$\n\nHere, $\\rm{Case}$$_i$ represents the $i$\\-th test case.  \nEach test case is given in the following format:\n\n$A_1$ $A_2$ $A_3$ $A_4$ $A_5$\n$P_1$ $P_2$ $P_3$ $P_4$ $P_5$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc174_b","tags":[],"sample_group":[["6\n1 0 1 0 0\n1 2 3 4 5\n0 2 2 0 0\n1 1 1 1 5\n0 1 2 0 0\n1 1 1 5 3\n1 1 1 0 0\n1 1 1 1 1\n0 0 0 0 1\n1 1 1 1 1\n100000000 100000000 100000000 0 0\n100000000 100000000 100000000 100000000 100000000","5\n2\n3\n2\n0\n15000000000000000\n\nThis input contains six test cases.\n\n*   For the first test case, you can, for example, do the following to have an average rating of at least $3$ stars with a bribe of $5$ yen, which is the minimum possible amount.\n    *   Initially, there are $1,0,1,0,0$ reviews with $1,2,3,4,5$ stars, respectively.\n    *   Pay a bribe of $P_5 = 5$ yen to add one $5$\\-star review.\n    *   As a result, there are $1,0,1,0,1$ reviews with $1,2,3,4,5$ stars, respectively, averaging $3$ stars.\n*   For the second test case, you can, for example, do the following to have an average rating of at least $3$ stars with a bribe of $2$ yen, which is the minimum possible amount.\n    *   Initially, there are $0,2,2,0,0$ reviews with $1,2,3,4,5$ stars, respectively.\n    *   Pay a bribe of $P_4 \\times 2 = 2$ yen to add two $4$\\-star reviews.\n    *   As a result, there are $0,2,2,2,0$ reviews of with $1,2,3,4,5$ stars, respectively, averaging $3$ stars.\n*   For the third test case, you can, for example, do the following to have an average rating of at least $3$ stars with a bribe of $3$ yen, which is the minimum possible amount.\n    *   Initially, there are $0,1,2,0,0$ reviews with $1,2,3,4,5$ stars, respectively.\n    *   Pay a bribe of $P_5 = 3$ yen to add one $5$\\-star review.\n    *   As a result, there are $0,1,2,0,1$ reviews with $1,2,3,4,5$ stars, respectively, averaging $3.25$ stars.\n*   For the fourth test case, you can, for example, do the following to have an average rating of at least $3$ stars with a bribe of $2$ yen, which is the minimum possible amount.\n    *   Initially, there are $1,1,1,0,0$ reviews with $1,2,3,4,5$ stars, respectively.\n    *   Pay a bribe of $P_4 = 1$ yen to add one $4$\\-star review.\n    *   Pay a bribe of $P_5 = 1$ yen to add one $5$\\-star review.\n    *   As a result, there are $1,1,1,1,1$ reviews with $1,2,3,4,5$ stars, respectively, averaging $3$ stars.\n*   For the fifth test case, you can, for example, do the following to have an average rating of at least $3$ stars with a bribe of $0$ yen, which is the minimum possible amount.\n    *   Initially, there are $0,0,0,0,1$ reviews with $1,2,3,4,5$ stars, respectively.\n    *   Since the average is already $5$, which is not less than $3$, give no bribe.\n*   For the sixth test case, note that the answer may not fit into a $32$\\-bit signed integer."]],"created_at":"2026-03-03 11:01:14"}}