Submission #167555

#TimeUsernameProblemLanguageResultExecution timeMemory
167555combi1k1Vještica (COCI16_vjestica)C++14
160 / 160
371 ms1656 KiB
#include<bits/stdc++.h>

using namespace std;

const int   inf = 0x3f3f3f3f;

vector<int> s[16];

int A[1 << 16];
int f[1 << 16];

int work(int m) {
    if (m == 0)     return  0;
    if (f[m] < inf) return  f[m];

    if (__builtin_popcount(m) == 1)
        return  f[m] = A[m];

    for(int x = m ; x ; x = (x - 1) & m)    if (x ^ m)
        f[m] = min(f[m],work(x) + work(m ^ x) - A[m]);

    return  f[m];
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;

    for(int i = 0 ; i < n ; ++i)    {
        s[i].resize(26);
        string t;   cin >> t;

        for(char c : t)
            s[i][c - 'a']++;
    }

    for(int i = 1 ; i < (1 << n) ; ++i) {
        vector<int> c(26,1e6);
        for(int j = 0 ; j < n ; ++j)    if (i >> j & 1)
        for(int k = 0 ; k < 26 ; ++k)   c[k] = min(c[k],s[j][k]);

        for(int x : c)
            A[i] += x;
    }

    memset(f,0x3f3f3f3f,sizeof f);

    cout << work((1 << n) - 1) + 1 << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...