# Is there an efficient algorithm for finding rows that have the same sum of column values up to a threshold value?

I have a table like this (more specifically a nested dictionary)

img a b c d e
1 img 0 0 1 0 1
2 img 3 1 0 0 2 3 img 4 0 0 0 1
4 img 2 2 0 0 0
5 img 0 0 0 0 2
6 img 2 2 0 0 0 7 img 1 0 0 1 0

I need to add all the column names upto a threshold value, I need those rows which would give me the same sum for all columns.

for example, If I keep 5 as a threshold then it should give rows which sums upto 5 (5 a-> 5, b->5, c->5, d->5, e->5, f->5) and as it iterates through each rows, column should not exceed the threshold.

One way I thought of this was to add iteratively until one class fills up and then stop counting for that class. I n my case

a b c d e 1img 0 0 1 0 1 2img 3 1 1 0 3 3img 7 0 0 0 1 (skips this as this exceeds the threshold) 4img 5 3 1 0 3 (a is full, ignores entire row for which has value for class a) 5img 0 3 1 0 5 (e is full, ignores entire row for which has value for class a and e) 6img 2 2 0 0 0 (since now a will become 7, it skips this step and jumps to next row) and it should go on like that and finally returns those img which will satisfy the conditions. Sometimes the constraint cannot fill upto 5, so maybe at the end I try with 1 or 2 value more than threshold.

Will it work if I consider the class name such a,b,c... as nodes and values as weight of nodes and consider any graphical approach? Also, I don't want to use pandas dataframe.