How do we get both the minimum value and the "path" of reaching this value in a dp solution?

Actual problem : https://codeforces.com/problemset/problem/1256/E

my dp solution for value : https://ideone.com/5v9BTW

my problem : along with the value they have asked for the team formation. Help me with how to find the team formations.