tinycodinglm / misc /misc-0234.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
4.24 kB
{"type": "session", "harness": "opencode", "id": "misc-0234", "name": "Competitive Programming"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code. When solving problems, explain your reasoning step by step before providing the final solution."}}
{"type": "message", "message": {"role": "user", "content": "\"Bless us and splash us , precious . Thats a meaty mouthful . \" said Gollum on seeing \nBilbo Baggins . Poor Bilbo Baggins is lost in the Misty Mountains and worst of all now he is \nstuck with Gollum who wants to finish him off. As Mr. Bilbo Baggins is clever , he convinced \nGollum to play a game of riddles where if Bilbo answers correctly to the riddle given by Gollum ,\nhe wins and Gollum shows him the way out of the mountains . But if he loses , then Golumn would \nfinish him off. Gollum gives Bilbo a fixed quantity of sand , say x units and also gives \nthe quantity of sand required for each digit (1-9) , i.e he gives 9 numbers (x1,x2...x9) where xi \nrepresents sand required for ith digit . \nNow he ask Baggins to output the largest number possible using the given x units of sand.\nBilbo Baggins find this problem quite difficult and thus seeks your help .\n\nYour task is simply to output the largest number possible which can be formed using x units of sand , given\nsand required for each digit. If its impossible to form a number, print '-1'.\n \n\nInput\nFirst line contains integer T , the number of test cases. T testcases follow. The first line of each \ntestcase contains integer n. Following n lines contain 9 integers , the amount of sand required for each number. \n\nOutput\nFor each test case print largest number possible, if not possible to form the number , print -1 .\n \n\nConstraints\n1 ≤ T ≤ 10\n1 ≤n ≤ 10^6\n1 ≤xi ≤ 10^5\n\nSAMPLE INPUT\n3\n5\n9 4 2 2 6 3 2 2 1\n2\n5 11 2 2 5 8 9 10 19\n5\n9 9 9 9 9 9 9 9 9\n\nSAMPLE OUTPUT\n99999\n4\n-1\n\nExplanation\n\nSAMPLE CASE 1 :\nMaximum sand available is 5 units . So maximum number that can be formed using 5 units of sand is 99999 where each digit '9' consumed 1 unit of sand.\n\nSAMPLE CASE 3 :\nMaximum available sand is 5 units but sand required to form any of the 9 digits ( 1 to 9) is greater than maximum available sand which is 9 units . So its impossible to form any number and you have to print '-1' (without quotes).\n\nNOTE : Ignore '0' digit . You have to form numbers without considering '0' digit."}}
{"type": "message", "message": {"role": "assistant", "content": "import sys\n\ndef main():\n input = sys.stdin.read().split()\n idx = 0\n T = int(input[idx])\n idx += 1\n for _ in range(T):\n n = int(input[idx])\n idx += 1\n costs = list(map(int, input[idx:idx+9]))\n idx += 9\n min_cost = min(costs)\n if min_cost > n:\n print(-1)\n continue\n # Find the highest digit with min_cost\n m = 0\n for d in range(9, 0, -1):\n if costs[d-1] == min_cost:\n m = d\n break\n K_max = n // min_cost\n remaining_sand = n - K_max * min_cost\n counts = [0] * 10 # 1-based index for digits 1-9\n counts[m] = K_max\n # Process higher digits to replace m where possible\n for h in range(9, m, -1):\n cost_h = costs[h-1]\n delta = cost_h - min_cost\n if delta <= 0:\n continue\n if delta > remaining_sand:\n continue\n possible = remaining_sand // delta\n possible = min(possible, counts[m])\n if possible > 0:\n counts[h] += possible\n counts[m] -= possible\n remaining_sand -= possible * delta\n if remaining_sand == 0:\n break\n # Build the result string\n result = []\n for d in range(9, 0, -1):\n if counts[d] > 0:\n result.append(str(d) * counts[d])\n print(''.join(result) if result else '-1')\n\nif __name__ == \"__main__\":\n main()"}}