Artifact Content
Not logged in

Artifact 832763487ee7bc8f4439ef7b89739f0d83d0c2ee


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

enum Type {X, SUM, MAX};
struct Tree {
	Type type;
	Tree* left;
	Tree* right;
	int size;
	~Tree() { delete left; delete right; }
};

class CircuitsConstruction { public:
	int maximizeResistance(string circuit, vector <int> conductors)
	{
		sort(conductors.rbegin(), conductors.rend());
		const char* p = circuit.c_str();
		Tree* t = parse(p);
		int ans = solve(t, conductors);
		delete t;
		return ans;
	}

	Tree* parse(const char*& p)
	{
		char tc = *p++;
		Tree* t = new Tree;
		t->type = (tc=='X' ? X : tc=='A' ? SUM : MAX);
		if(tc=='X') {
			t->left = t->right = 0;
			t->size = 1;
		} else {
			t->left = parse(p);
			t->right = parse(p);
			t->size = t->left->size + t->right->size;
		}
		return t;
	}

	int plusdepth(Tree* t)
	{
		switch(t->type)
		{
		case X:
			return 0;
		case SUM:
			return plusdepth(t->left) + plusdepth(t->right) + 1;
		case MAX:
			return max(plusdepth(t->left), plusdepth(t->right));
		}
	}

	int solve(Tree* t, const vector<int>& c)
	{
		int n = plusdepth(t)+1;
		return accumulate(c.begin(), c.begin()+n, 0);
	}
};

// 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 int& Expected, const int& 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(_, CircuitsConstruction().maximizeResistance(circuit, conductors));}
int main(){

CASE(0)
	string circuit = "BXBXX"; 
	int conductors_[] = {8, 2, 3};
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = 8; 
END
CASE(1)
	string circuit = "AAXXAXAXX"; 
	int conductors_[] = {1, 1, 2, 8, 10};
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = 22; 
END
CASE(2)
	string circuit = "AXBXX"; 
	int conductors_[] = {8, 2, 3};
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = 11; 
END
CASE(3)
	string circuit = "BAAXBXXBXAXXBBAXXBXXAAXXX"; 
	int conductors_[] = {17, 7, 21, 102, 56, 72, 88, 15, 9, 192, 16, 8, 30};
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = 454; 
END
/*
CASE(4)
	string circuit = ; 
	int conductors_[] = ;
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = ; 
END
CASE(5)
	string circuit = ; 
	int conductors_[] = ;
	  vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 
	int _ = ; 
END
*/
}
// END CUT HERE