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 : 5v9BTW - Online C++0x Compiler & Debugging Tool - Ideone.com

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