// File: /workspace/github_cpp_makefile_star2_O2/Laakeri[P]Triangulator/src/bayes.cpp
// Function: Bayes::SolveGraph::solveGraph(Graph const&, long, long, std::vector<long, std::allocator<long>>)
pair<int64_t, vector<pair<int, int> > > solveGraph(const Graph& G, int64_t lowerBound, int64_t upperBound, vector<int64_t> tableSizes) {
	assert(lowerBound >= 0);
	assert(lowerBound <= upperBound);
	assert((int)tableSizes.size() == G.n);
	for (int i = 0; i < G.n; i++) {
		for (int nx : G.g[i]) {
			lowerBound = max(lowerBound, tableSizes[i]*tableSizes[nx]);
		}
	}
	
	auto fill = MCS::MCS_MP(G);
	
	Graph gFill = G;
	gFill.addEdges(fill.F);
	
	int64_t fillWidth = MCS::getBayesWidth(gFill, tableSizes);
	
	assert(fillWidth >= lowerBound);
	if (fillWidth <= lowerBound) return {fillWidth, fill.F};
	
	if (fill.F.size() <= 0) {
		if (fillWidth > upperBound) return {-1, FAILSOLUTION};
		return {fillWidth, fill.F};
	}
	
	vector<Graph> atoms = MCS::getAtoms(G, gFill, fill).S;
	auto cmp = [](const Graph& a, const Graph& b) {
		return a.n > b.n;
	};
	sort(atoms.begin(), atoms.end(), cmp);
	pair<int64_t, vector<pair<int, int> > > sol = {0, {}};
	for (int i = 0; i < (int)atoms.size(); i++) {
		pair<int64_t, vector<pair<int, int> > > tSol;
		if (i+1 < (int)atoms.size()) {
			tSol = SolveAtom::solveAtom(atoms[i], 0, upperBound - sol.F, atoms[i].mapTableInto(tableSizes));
		}
		else {
			tSol = SolveAtom::solveAtom(atoms[i], max((int64_t)0, lowerBound - sol.F), upperBound - sol.F, atoms[i].mapTableInto(tableSizes));
		}
		if (tSol.S == FAILSOLUTION) {
			if (fillWidth <= upperBound) {
				return {fillWidth, fill.F};
			}
			else {
				return {-1, FAILSOLUTION};
			}
		}
		sol.F += tSol.F;
		if (sol.F > upperBound) return {-1, FAILSOLUTION};
		tSol.S = atoms[i].mapBack(tSol.S);
		sol.S.insert(sol.S.end(), tSol.S.begin(), tSol.S.end());
	}
	assert(sol.F <= upperBound);
	return sol;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r8, 0x8(%rsp)
testq %rdx, %rdx
js 0x45ff
movq %rcx, %r14
movq %rdx, %r13
cmpq %rcx, %rdx
jg 0x4618
movq %rsi, %rbx
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
cmpl %ecx, 0x50(%rsi)
jne 0x4631
movq %rdi, %r15
movq 0x38(%rbx), %rdx
xorl %esi, %esi
testl %ecx, %ecx
cmovlel %esi, %ecx
cmpq %rcx, %rsi
je 0x4184
imulq $0x18, %rsi, %r8
movq (%rdx,%r8), %rdi
movq 0x8(%rdx,%r8), %r8
cmpq %r8, %rdi
je 0x417f
movslq (%rdi), %r9
movq (%rax,%r9,8), %r9
imulq (%rax,%rsi,8), %r9
cmpq %r9, %r13
cmovleq %r9, %r13
addq $0x4, %rdi
jmp 0x4161
incq %rsi
jmp 0x414f
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0xe30c
leaq 0x130(%rsp), %rdi
movq %rbx, %rsi
callq 0x5152
leaq 0x130(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x7cf8
leaq 0x130(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xefd6
cmpq %r13, %rax
jl 0x464a
jle 0x42b0
movq 0xa0(%rsp), %rcx
cmpq 0x98(%rsp), %rcx
je 0x42cc
movq %rax, 0x80(%rsp)
movq %r14, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x130(%rsp), %rdx
leaq 0x98(%rsp), %rcx
movq %rbx, %rsi
callq 0xe775
leaq 0x48(%rsp), %rbp
movups (%rbp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0x70(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
andq $0x0, 0x10(%rbp)
movq %rbp, %rdi
callq 0x52ce
movq 0x60(%rsp), %r14
movq 0x68(%rsp), %rbx
cmpq %rbx, %r14
je 0x42f9
movq %rbx, %r12
subq %r14, %r12
movq %r12, %rax
cqto
pushq $0x58
popq %rcx
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4c71
cmpq $0x581, %r12            # imm = 0x581
jl 0x42ee
leaq 0x580(%r14), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x506b
jmp 0x42a9
movq %r12, %rdi
callq 0x5101
addq $0x58, %r12
cmpq %rbx, %r12
jne 0x429d
jmp 0x42f9
movq %rax, (%r15)
movq %r15, %rdi
addq $0x8, %rdi
leaq 0x98(%rsp), %rsi
callq 0x35f2
jmp 0x45d0
leaq 0x8(%r15), %rdi
cmpq %r14, %rax
jle 0x4599
orq $-0x1, (%r15)
leaq 0x2bf14(%rip), %rsi     # 0x301f8
callq 0x35f2
jmp 0x45d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x506b
movq %r15, 0x38(%rsp)
leaq 0x10(%rsp), %rsi
andq $0x0, 0x10(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
andq $0x0, 0x40(%rsp)
movq %rbp, %rdi
callq 0x35f2
leaq 0x10(%rsp), %rdi
callq 0x3bfe
leaq 0xe0(%rsp), %rbx
leaq 0xc8(%rsp), %r15
xorl %r14d, %r14d
xorl %esi, %esi
movq 0x60(%rsp), %r12
movq 0x68(%rsp), %rax
subq %r12, %rax
cqto
pushq $0x58
popq %rcx
idivq %rcx
movslq %eax, %rcx
cmpq %rcx, %rsi
jge 0x4511
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rsi, 0x88(%rsp)
leal 0x1(%rsi), %ecx
movq %r14, 0x90(%rsp)
addq %r14, %r12
cmpl %eax, %ecx
jge 0x43f3
movq 0x40(%rsp), %rbp
leaq 0x118(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0x5202
movq (%rsp), %rcx
subq %rbp, %rcx
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r14, %r8
callq 0x3e18
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5292
leaq 0xe8(%rsp), %rdi
callq 0x3bfe
movq %r14, %rdi
callq 0x6ebc
leaq 0x48(%rsp), %rbp
movq (%rsp), %r12
leaq 0x2be0f(%rip), %rsi     # 0x301f8
leaq 0xc8(%rsp), %r15
jmp 0x4469
movq 0x40(%rsp), %rbp
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0x5202
movq %r13, %rdx
subq %rbp, %rdx
testq %rdx, %rdx
movl $0x0, %eax
cmovleq %rax, %rdx
movq (%rsp), %rcx
subq %rbp, %rcx
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %r8
callq 0x3e18
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5292
leaq 0xe8(%rsp), %rdi
callq 0x3bfe
movq %r14, %rdi
callq 0x6ebc
leaq 0x48(%rsp), %rbp
movq (%rsp), %r12
leaq 0x2bd8f(%rip), %rsi     # 0x301f8
leaq 0x18(%rsp), %rdi
callq 0x35c8
movq 0x90(%rsp), %r14
testb %al, %al
jne 0x4549
movq 0x40(%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq %r12, %rax
jg 0x4576
movq 0x60(%rsp), %r12
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x35f2
addq %r14, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x82b2
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3d74
movq %rbx, %rdi
callq 0x3bfe
movq %r15, %rdi
callq 0x3bfe
movq 0x50(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %rbp, %rdi
callq 0x52ae
movq %r12, %rdi
callq 0x3bfe
addq $0x58, %r14
movq 0x88(%rsp), %rsi
incq %rsi
jmp 0x433b
movq 0x40(%rsp), %rax
cmpq (%rsp), %rax
jg 0x4667
movq 0x38(%rsp), %r15
movq %rax, (%r15)
movups 0x48(%rsp), %xmm0
movups %xmm0, 0x8(%r15)
movq 0x58(%rsp), %rax
movq %rax, 0x18(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
andq $0x0, 0x10(%rbp)
jmp 0x45be
movq 0x38(%rsp), %r15
leaq 0x8(%r15), %rdi
movq 0x80(%rsp), %rax
cmpq %r12, %rax
jle 0x45a1
orq $-0x1, (%r15)
leaq 0x2bc8e(%rip), %rsi     # 0x301f8
leaq 0x18(%rsp), %rbx
callq 0x35f2
jmp 0x45b6
movq 0x38(%rsp), %r15
orq $-0x1, (%r15)
movq %r15, %rdi
addq $0x8, %rdi
leaq 0x2bc6b(%rip), %rsi     # 0x301f8
leaq 0x18(%rsp), %rbx
callq 0x35f2
jmp 0x45b6
movq %rax, (%r15)
jmp 0x42ba
movq %rax, (%r15)
leaq 0x98(%rsp), %rsi
leaq 0x18(%rsp), %rbx
callq 0x35f2
movq %rbx, %rdi
callq 0x3bfe
movq %rbp, %rdi
callq 0x3bfe
leaq 0x60(%rsp), %rdi
callq 0x52ce
leaq 0x130(%rsp), %rdi
callq 0x36d2
leaq 0x98(%rsp), %rdi
callq 0x3642
movq %r15, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bd48(%rip), %rdi     # 0x2034e
leaq 0x1bc9b(%rip), %rsi     # 0x202a8
leaq 0x1bd4a(%rip), %rcx     # 0x2035e
pushq $0x33
jmp 0x4661
leaq 0x1bdb5(%rip), %rdi     # 0x203d4
leaq 0x1bc82(%rip), %rsi     # 0x202a8
leaq 0x1bd31(%rip), %rcx     # 0x2035e
pushq $0x34
jmp 0x4661
leaq 0x1bdb5(%rip), %rdi     # 0x203ed
leaq 0x1bc69(%rip), %rsi     # 0x202a8
leaq 0x1bd18(%rip), %rcx     # 0x2035e
pushq $0x35
jmp 0x4661
leaq 0x1bc3f(%rip), %rdi     # 0x20290
leaq 0x1bc50(%rip), %rsi     # 0x202a8
leaq 0x1bcff(%rip), %rcx     # 0x2035e
pushq $0x43
popq %rdx
callq 0x2150
leaq 0x1bd9d(%rip), %rdi     # 0x2040b
leaq 0x1bc33(%rip), %rsi     # 0x202a8
leaq 0x1bce2(%rip), %rcx     # 0x2035e
pushq $0x66
jmp 0x4661
jmp 0x46de
jmp 0x46de
jmp 0x46a0
jmp 0x46de
movq %rax, %rbx
jmp 0x46f5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x46f0
jmp 0x46a0
jmp 0x46a0
movq %rax, %rbx
jmp 0x470c
movq %rax, %rbx
jmp 0x46ff
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
jmp 0x46bf
jmp 0x46de
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x6ebc
jmp 0x46e1
jmp 0x46de
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x3bfe
jmp 0x46e1
jmp 0x46de
jmp 0x46de
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3bfe
leaq 0x48(%rsp), %rdi
callq 0x3bfe
leaq 0x60(%rsp), %rdi
callq 0x52ce
leaq 0x130(%rsp), %rdi
callq 0x36d2
leaq 0x98(%rsp), %rdi
callq 0x3642
movq %rbx, %rdi
callq 0x2340