1316E - Team Building - CodeForces Solution


bitmasks dp greedy sortings *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);

// imp numbers
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;
const ll N = 1e6;

int main()
{
    fast;
    ll n, p, k;
    cin >> n >> p >> k;
    vector<pair<ll, ll>> vecp(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> vecp[i].first;
        vecp[i].second = i;
    }
    ll score[n][p];
    for (ll i = 0; i < n; i++)
    {
        for (ll j = 0; j < p; j++)
        {
            cin >> score[i][j];
        }
    }
    sort(rall(vecp));
    ll dp[n + 1][(1LL << p) + 1];
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (ll i = 1; i <= n; i++)
    {
        for (ll mask = 0; mask < (1LL << p); mask++)
        {
            ll selected = 0;

            // don't select this player in team
            ll remain = i - 1 - __builtin_popcount(mask);
            if (remain >= k)
            {
                if (dp[i - 1][mask] != -1)
                {
                    dp[i][mask] = dp[i - 1][mask];
                }
            }
            else
            {
                if (dp[i - 1][mask] != -1)
                {
                    dp[i][mask] = dp[i - 1][mask] + vecp[i - 1].first;
                }
            }
            // select at some position
            for (ll j = 0; j < p; j++)
            {
                if ((mask & (1 << j)) && dp[i - 1][(mask ^ (1LL << j))] != -1)
                {
                    dp[i][mask] = max(dp[i][mask], dp[i - 1][(mask ^ (1LL << j))] + score[vecp[i - 1].second][j]);
                }
            }
        }
    }
    cout << dp[n][(1LL << p) - 1] << "\n";
}
// NineFathoms


Comments

Submit
0 Comments
More Questions

1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings
1281A - Suffix Three
1421C - Palindromifier
1443A - Kids Seating
963A - Alternating Sum
1191B - Tokitsukaze and Mahjong
1612G - Max Sum Array
1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple