Artifact Content
Not logged in

Artifact 5e2ee475015aa982fb99b0add460232414239988


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

static const int N = 60;

typedef vector<int> Num;
Num conv( const string& s )
{
	Num n(N);
	for(int i=0; i<s.size(); ++i)
	{
		char c = s[s.size()-1-i];
		n[N-1-i] = ('0'<=c&&c<='9' ? c-'0' : c-'A'+10);
	}
	return n;
}

Num add( const Num& a, const Num& b )
{
	Num n(N);
	int c = 0;
	for(int i=0; i<N; ++i)
	{
		int s = a[N-1-i] + b[N-1-i] + c;
		n[N-1-i] = s%36;
		c = s/36;
	}
	return n;
}

class HexatridecimalSum {
public:
	string maximizeSum(vector <string> numbers, int k) 
	{
		vector<Num> ns;
		transform( numbers.begin(), numbers.end(), back_inserter(ns), &conv );

		vector<Num> gain(36);
		for(int d=0; d<36; ++d)
		{
			vector<int> g(N);
			for(int i=0; i<ns.size(); ++i)
				for(int j=0,f=0; j<N; ++j) {
					if( ns[i][j] ) f=1;
					if( f && ns[i][j] == d )
						g[j] += (35-d);
				}
			Num gg(N);
			for(int j=0; j<N; ++j) {
				int x = g[N-1-j];
				gg[N-1-j] = x % 36;
				if(x/36) g[N-1-j-1] += x/36;
			}
			gain[d] = gg;
		}
		sort( gain.rbegin(), gain.rend() );
		Num zero(N);
		Num n = accumulate( gain.begin(), gain.begin()+k, zero, &add );
		Num m = accumulate( ns.begin(), ns.end(), n, &add );

		string ans;
		for(int i=0,f=0; i<N; ++i)
		{
			if(m[i]) f=1;
			if(f)
				if( m[i]<=9 )
					ans += char(m[i]+'0');
				else
					ans += char(m[i]-10+'A');
		}
		return ans;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string numbers_[] = {"HELLO"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	int k = 2; 
	string RetVal = "ZZLLO"; 
	return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); }
int Test_(Case_<1>) {
	string numbers_[] = {"500", "POINTS", "FOR", "THIS", "PROBLEM"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	int k = 5; 
	string RetVal = "1100TC85"; 
	return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); }
int Test_(Case_<2>) {
	string numbers_[] = {"TO", "BE", "OR", "NOT", "TO", "BE"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	int k = 0; 
	string RetVal = "QNO"; 
	return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); }
int Test_(Case_<3>) {
	string numbers_[] = {"KEQUALS36"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	int k = 36; 
	string RetVal = "ZZZZZZZZZ"; 
	return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); }
int Test_(Case_<4>) {
	string numbers_[] = {"GOOD", "LUCK", "AND", "HAVE", "FUN"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	int k = 7; 
	string RetVal = "31YUB"; 
	return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE