You manage a portfolio of n assets. You are given an integer total_funds, the money available to invest, and an integer array weights of length n, where weights[i] is the target weight of asset i. You are also given a d x n integer array price_changes, where price_changes[j][i] is the percentage change (an integer, possibly negative) in the price of asset i on day j.
On day 0, split total_funds across the assets in proportion to their weights using integer division. Any funds left over from the rounding go to the asset with the largest weight.
Then process the days in order. Each day, first apply that day's price changes to the value held in every asset, rounding down, and then rebalance: add up the new values and split that total across the assets with the same rule as day 0.
Return the allocation after all d days have been processed.
Function Signature
def final_allocations(total_funds: int, weights: list[int], price_changes: list[list[int]]) -> list[int]:
Rules
-
Allocation rule for a total
T
: asset
i
gets
floor(T * weights[i] / W)
, where
W = sum(weights)
. The leftover,
T
minus the sum of those amounts, is added to the asset with the largest weight. When several assets share the largest weight, the one with the smallest index gets the leftover.
-
Day 0 applies the allocation rule to
total_funds
.
-
Day
j
, for
j
from 1 to
d
, first sets
value[i] = floor(value[i] * (100 + price_changes[j-1][i]) / 100)
for every asset, then applies the allocation rule to
T = sum(value)
.
-
When
d = 0
, return the day-0 allocation.
-
All arithmetic is exact integer arithmetic. Under the constraints the total can grow far past the 64-bit range (a +100% day doubles every value), and the expected output uses the exact values.
Constraints
-
1 <= n <= 1000
-
1 <= total_funds <= 10^9
-
1 <= weights[i] <= 10^4
-
0 <= d <= 365
-
-50 <= price_changes[j][i] <= 100
-
len(price_changes) == d
and
len(price_changes[j]) == n
Examples
Input: total_funds = 1000, weights = [3, 2, 1], price_changes = [[10, -5, 0], [0, 20, -10]]
Output: [542, 361, 180]
Day 0 splits 1000 into [500, 333, 166] and gives the leftover 1 to index 0, so the values are [501, 333, 166]; day 1 turns them into [551, 316, 166] (total 1033), which rebalances to [517, 344, 172]. Day 2 turns those into [517, 412, 154] (total 1083), which splits as [541, 361, 180] with the leftover 1 again going to index 0.
Input: total_funds = 100, weights = [1, 1], price_changes = []
Output: [50, 50]
With no days, the answer is the day-0 allocation. Each asset gets floor(100 * 1 / 2) = 50 and nothing is left over.
Input: total_funds = 10, weights = [1, 3, 3], price_changes = [[-50, 10, 100]]
Output: [1, 7, 5]
Indices 1 and 2 tie for the largest weight, so leftovers go to index 1, and day 0 gives [1, 4, 4] plus 1 for index 1, which is [1, 5, 4]. Day 1 rounds the values down to [0, 5, 8] (total 13), which splits as [1, 5, 5] with 2 left over for index 1.