Artifact b1fd16df13023b5a14ff5df5f1c5f07e1b2fe55b
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class PrefixFreeSuperset { public:
long long minSumLength(vector <string> cur, long long k)
{
map<int,LL> open; {
string me("");
rec(me, cur, open);
}
LL totalLen = 0;
for(int i=0; i<cur.size(); ++i)
totalLen += cur[i].size();
k -= cur.size();
while(k) {
if( open.empty() )
return -1;
map<int,LL>::iterator it = open.begin();
int lll = it->first;
LL ccc = it->second;
open.erase(it);
if( k <= ccc ) {
totalLen += lll*k;
k = 0;
}
else if( k <= ccc + open[lll+1] ) {
totalLen += lll*ccc;
k -= ccc;
}
else if( k <= ccc*2 + open[lll+1] ) {
LL div = k - (ccc+open[lll+1]);
open[lll+1] += div*2;
totalLen += (ccc-div)*lll;
k -= (ccc-div);
}
else
open[lll+1] += ccc*2;
}
return totalLen;
}
void rec(string& me, const vector<string>& prefix, map<int,LL>& open)
{
bool iamprefix = false;
for(int i=0; i<prefix.size(); ++i)
if( me == prefix[i] )
return;
else if( is_prefix(me, prefix[i]) )
iamprefix = true;
if( iamprefix )
{
me += '0';
rec(me, prefix, open);
me.resize(me.size()-1);
me += '1';
rec(me, prefix, open);
me.resize(me.size()-1);
}
else
{
open[me.size()] ++;
}
}
bool is_prefix(const string& s, const string& t)
{
return s.size()<=t.size() && s==t.substr(0,s.size());
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, PrefixFreeSuperset().minSumLength(cur, k));}
int main(){
CASE(0)
string cur_[] = {"010"};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 4LL;
long long _ = 9LL;
END
CASE(1)
string cur_[] = {"01","000"};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 4LL;
long long _ = 9LL;
END
CASE(2)
string cur_[] = {"0011","011110101","11101010111","11101010100000000","11101010100000001111"};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 1000000000000LL;
long long _ = 39971901640560LL;
END
CASE(3)
string cur_[] = {"010","00","011","1"};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 4LL;
long long _ = 9LL;
END
CASE(4)
string cur_[] = {"010","00","011","1"};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 5LL;
long long _ = -1LL;
END
CASE(5)
string cur_[] = {"0011010",
"0011011110",
"00110111001",
"0011011111",
"00110111000",
};
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = 9LL;
long long _ = 58LL;
END
/*
CASE(6)
string cur_[] = ;
vector <string> cur(cur_, cur_+sizeof(cur_)/sizeof(*cur_));
long long k = LL;
long long _ = LL;
END
*/
}
// END CUT HERE