{"problem":{"name":"K. Lending Woes","description":{"content":"Lending money to friends can be problematic. In this task you will simplify an already problematic situation by solving a problem. Imagine a group of $N$ people numbered from $0$ to $N -1$, each of w","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":1000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF10240K"},"statements":[{"statement_type":"Markdown","content":"Lending money to friends can be problematic. In this task you will simplify an already problematic situation by solving a problem.\n\nImagine a group of $N$ people numbered from $0$ to $N -1$, each of which has been lending money to each other. However, they now want to settle all their debts and would like to do so in the most efficient manner.\n\nNotice that for example, if person A lent $1 to person B and then person B lent $1 to person C, the most efficient way to settle the group's debts would be to have A pay C $1 directly.\n\nGiven a list of loans, find a sequence of payments such that the debts are settled. Moreover, this sequence of transactions must minimize the total amount of money transferred, and additionally minimize the number of transactions needed. (That is, first minimize the total amount, and then minimize the number of transactions.)\n\nIn the first line two integers, $N <= 18$ and $K <= 10^5$, representing the number of people and loans that happened between them.\n\nThe next $K$ lines contain three integers each: $a$, $b$, and $1 <= c <= 1000$, representing that $a$ lent $b$ $$ c$.\n\nIn the first line one integer, $T <= 1000$, the amount of transactions needed to settle the debts.\n\nThe next $T$ lines contain three integers each: $a$, $b$, and $1 <= c <= 10^5$, representing that $a$ should pay $b$ $$ c$.\n\n## Input\n\nIn the first line two integers, $N <= 18$ and $K <= 10^5$, representing the number of people and loans that happened between them.The next $K$ lines contain three integers each: $a$, $b$, and $1 <= c <= 1000$, representing that $a$ lent $b$ $$ c$.\n\n## Output\n\nIn the first line one integer, $T <= 1000$, the amount of transactions needed to settle the debts.The next $T$ lines contain three integers each: $a$, $b$, and $1 <= c <= 10^5$, representing that $a$ should pay $b$ $$ c$.\n\n[samples]","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ N \\in \\mathbb{Z} $, $ 1 \\leq N \\leq 18 $, be the number of people, indexed from $ 0 $ to $ N-1 $.  \nLet $ K \\in \\mathbb{Z} $, $ 1 \\leq K \\leq 10^5 $, be the number of loans.  \nLet $ L = \\{(a_i, b_i, c_i) \\mid i \\in \\{1, \\dots, K\\}\\} $ be the set of loans, where $ a_i $ lent $ b_i $ an amount $ c_i \\in \\mathbb{Z}^+ $, $ 1 \\leq c_i \\leq 1000 $.\n\nDefine the net balance $ d_j \\in \\mathbb{Z} $ for person $ j \\in \\{0, \\dots, N-1\\} $:  \n$$\nd_j = \\sum_{\\substack{(a, b, c) \\in L \\\\ a = j}} c - \\sum_{\\substack{(a, b, c) \\in L \\\\ b = j}} c\n$$\n\nLet $ D = \\{ d_j \\mid j \\in \\{0, \\dots, N-1\\} \\} $ be the vector of net balances.\n\n**Constraints**  \n1. $ \\sum_{j=0}^{N-1} d_j = 0 $ (debts are balanced).  \n2. Each transaction $ (a, b, c) $ must satisfy $ d_a > 0 $, $ d_b < 0 $, and $ c > 0 $.  \n3. After all transactions, $ d_j = 0 $ for all $ j $.\n\n**Objective**  \nFind a set of transactions $ T = \\{(a_i, b_i, c_i)\\}_{i=1}^m $ minimizing:  \n1. First: total amount transferred $ \\sum_{i=1}^m c_i $,  \n2. Second: number of transactions $ m $.  \n\nOutput $ m $ and the list of $ m $ transactions.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF10240K","tags":[],"sample_group":[],"created_at":"2026-03-03 11:00:39"}}