// File: /workspace/github_cpp_makefile_star2_O1/Laakeri[P]Triangulator/src/MCS.cpp
// Function: MCS::getBayesWidth(Graph const&, std::vector<long, std::allocator<long>> const&)
int64_t getBayesWidth(const Graph& G, const vector<int64_t>& tableSizes) {
	if (G.n <= 1) return 0;
	vector<int> u(G.n);
	vector<int> order;
	for (int i = 0; i < G.n; i++) {
		if (G.g[i].size() == 0) {
			order.push_back(i);
			u[i] = 1;
		}
		else if (u[i] == 0) {
			auto ord = MCS_L(G, i, u);
			for (int x : ord) {
				assert(u[x] == 0);
				u[x] = 1;
			}
			order.insert(order.end(), ord.begin(), ord.end());
		}
	}
	assert((int)order.size() == G.n);
	for (int i = 0; i < (int)order.size(); i++) {
		invOrd[order[i]] = i;
	}
	vector<int> fw(G.n);
	for (int i = 0; i < G.n; i++) {
		int x = order[i];
		for (int nx : G.g[x]) {
			if (invOrd[nx] > i) {
				fw[x]++;
			}
		}
	}
	vector<int> noMaxi(G.n);
	int64_t bayesWidth = 0;
	for (int i = 0; i < G.n; i++) {
		int x = order[i];
		assert(0<=x&&x<G.n);
		vector<int> nb;
		for (int nx : G.g[x]) {
			if (invOrd[nx] > i) {
				nb.push_back(nx);
			}
		}
		auto cmp = [&](int a, int b) {
			return invOrd[a] < invOrd[b];
		};
		sort(nb.begin(), nb.end(), cmp);
		for (int ii = 0; ii < (int)nb.size(); ii++) {
			assert(fw[nb[ii]] >= (int)nb.size()-ii-1);
			if (fw[nb[ii]] == (int)nb.size()-ii-1) {
				noMaxi[nb[ii]] = 1;
			}
		}
		if (!noMaxi[x]) {
			int64_t ts = tableSizes[x];
			for (int nx : nb) {
				if ((long double)ts*(long double)tableSizes[nx] > (long double)(maxBWidth)) {
					ts = maxBWidth;
					break;
				}
				ts *= tableSizes[nx];
			}
			bayesWidth += ts;
			if (bayesWidth > maxBWidth) bayesWidth = maxBWidth;
		}
	}
	return bayesWidth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, 0x90(%rsp)
movslq 0x50(%rdi), %rsi
cmpq $0x2, %rsi
jge 0x13a45
xorl %eax, %eax
jmp 0x13ef2
movq %rdi, %rax
leaq 0x78(%rsp), %rdi
movq %rax, 0x70(%rsp)
leaq 0x20(%rsp), %rbx
movq %rbx, %rdx
callq 0xa6ba
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0x70(%rsp), %rbx
movl $0x0, 0x50(%rsp)
movl 0x50(%rbx), %esi
testl %esi, %esi
jle 0x13b72
xorl %edx, %edx
leaq 0x20(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x38(%rsp), %r13
leaq 0x78(%rsp), %rbp
movslq %edx, %rax
movq 0x38(%rbx), %rcx
leaq (%rax,%rax,2), %rsi
movq 0x8(%rcx,%rsi,8), %rdi
cmpq (%rcx,%rsi,8), %rdi
je 0x13b28
movq 0x78(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x13b5d
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rcx
callq 0x134fd
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rcx
cmpq %rcx, %rdx
je 0x13b02
movq 0x78(%rsp), %rax
movq %rdx, %rsi
movslq (%rsi), %rdi
cmpl $0x0, (%rax,%rdi,4)
jne 0x13f23
movl $0x1, (%rax,%rdi,4)
addq $0x4, %rsi
cmpq %rcx, %rsi
jne 0x13ae5
movq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x15cde
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x13b5d
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
jmp 0x13b5d
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x13b41
movl %edx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x13b4c
movq %r15, %rdi
movq %r12, %rdx
callq 0x8fde
movslq 0x50(%rsp), %rax
movq 0x78(%rsp), %rcx
movl $0x1, (%rcx,%rax,4)
movl 0x50(%rsp), %edx
incl %edx
movl %edx, 0x50(%rsp)
movl 0x50(%rbx), %esi
cmpl %esi, %edx
jl 0x13a9b
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
cmpl %ecx, %esi
jne 0x13f61
testl %ecx, %ecx
jle 0x13bad
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
leaq 0x454a32(%rip), %rsi    # 0x4685d0
movslq (%rax,%rdx,4), %rdi
movl %edx, (%rsi,%rdi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x13b9e
movslq 0x50(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0xa6ba
movl 0x50(%rbx), %edi
testl %edi, %edi
jle 0x13c19
movq 0x20(%rsp), %rax
movq 0x38(%rbx), %rcx
xorl %edx, %edx
leaq 0x4549f7(%rip), %rsi    # 0x4685d0
movslq (%rax,%rdx,4), %rdi
leaq (%rdi,%rdi,2), %r9
movq (%rcx,%r9,8), %r8
movq 0x8(%rcx,%r9,8), %r9
cmpq %r9, %r8
je 0x13c0d
movq 0x38(%rsp), %r10
movslq (%r8), %r11
movslq (%rsi,%r11,4), %r11
cmpq %r11, %rdx
jge 0x13c04
incl (%r10,%rdi,4)
addq $0x4, %r8
cmpq %r9, %r8
jne 0x13bf4
incq %rdx
movslq 0x50(%rbx), %rdi
cmpq %rdi, %rdx
jl 0x13bd9
movslq %edi, %rsi
leaq 0x50(%rsp), %rdi
movq %rsp, %rdx
callq 0xa6ba
movl 0x50(%rbx), %eax
testl %eax, %eax
jle 0x13e85
xorl %r12d, %r12d
fldl 0x17063(%rip)           # 0x2aca0
fstpt 0xac(%rsp)
leaq 0x454985(%rip), %r14    # 0x4685d0
movq %rsp, %rbp
movq $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rcx
movslq (%rcx,%r12,4), %r13
testq %r13, %r13
js 0x13f42
cmpl %eax, %r13d
jge 0x13f42
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x38(%rbx), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %rbx
movq 0x8(%rax,%rcx,8), %r15
cmpq %r15, %rbx
je 0x13cdb
movslq (%rbx), %rax
movl %eax, 0x6c(%rsp)
movslq (%r14,%rax,4), %rcx
cmpq %rcx, %r12
jge 0x13cd5
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x13cc8
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x13cd5
movq %rbp, %rdi
leaq 0x6c(%rsp), %rdx
callq 0x8fde
addq $0x4, %rbx
jmp 0x13c9a
movq (%rsp), %r15
movq 0x8(%rsp), %rbp
cmpq %rbp, %r15
je 0x13d68
movq %rbp, %rbx
subq %r15, %rbx
movq %rbx, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1547b
cmpq $0x41, %rbx
jl 0x13d5d
leaq 0x40(%r15), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x156f6
cmpq %rbp, %rbx
je 0x13d68
movl (%rbx), %eax
movslq %eax, %rcx
movslq -0x4(%rbx), %rdx
movl (%r14,%rcx,4), %edi
movq %rbx, %rsi
cmpl (%r14,%rdx,4), %edi
jge 0x13d55
movq %rbx, %rsi
movl %edx, (%rsi)
movslq -0x8(%rsi), %rdx
movl (%r14,%rcx,4), %edi
addq $-0x4, %rsi
cmpl (%r14,%rdx,4), %edi
jl 0x13d41
movl %eax, (%rsi)
addq $0x4, %rbx
jmp 0x13d23
movq %r15, %rdi
movq %rbp, %rsi
callq 0x156f6
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, %r8
subq %rdi, %r8
shrq $0x2, %r8
testl %r8d, %r8d
movq 0x70(%rsp), %rbx
movq %rsp, %rbp
jle 0x13dc7
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl %r8d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
decl %r8d
xorl %r9d, %r9d
movslq (%rdi,%r9,4), %r10
movl (%rcx,%r10,4), %r11d
cmpl %r8d, %r11d
jl 0x13f04
jne 0x13dbc
movl $0x1, (%rdx,%r10,4)
incq %r9
decl %r8d
cmpq %r9, %rsi
jne 0x13da1
movq 0x50(%rsp), %rcx
cmpl $0x0, (%rcx,%r13,4)
jne 0x13e61
movq 0x90(%rsp), %rcx
movq (%rcx), %rdx
movq (%rdx,%r13,8), %rcx
cmpq %rax, %rdi
je 0x13e46
leaq 0x4(%rdi), %rsi
movabsq $0xde0b6b3a7640000, %r9 # imm = 0xDE0B6B3A7640000
movslq -0x4(%rsi), %r8
movq %rcx, 0x98(%rsp)
fildll 0x98(%rsp)
movq (%rdx,%r8,8), %r8
movq %r8, 0xa0(%rsp)
fildll 0xa0(%rsp)
fmulp %st, %st(1)
imulq %r8, %rcx
fldt 0xac(%rsp)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
cmovaq %r9, %rcx
ja 0x13e50
leaq 0x4(%rsi), %r8
cmpq %rax, %rsi
movq %r8, %rsi
jne 0x13df9
jmp 0x13e50
movabsq $0xde0b6b3a7640000, %r9 # imm = 0xDE0B6B3A7640000
addq 0x18(%rsp), %rcx
cmpq %r9, %rcx
cmovgeq %r9, %rcx
movq %rcx, 0x18(%rsp)
testq %rdi, %rdi
je 0x13e73
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
incq %r12
movslq 0x50(%rbx), %rax
cmpq %rax, %r12
jl 0x13c57
jmp 0x13e8e
movq $0x0, 0x18(%rsp)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x13ea5
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x13ebc
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x13ed3
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x13eed
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x18(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16f9a(%rip), %rdi     # 0x2aea5
leaq 0x16dbd(%rip), %rsi     # 0x2accf
leaq 0x16f49(%rip), %rcx     # 0x2ae62
movl $0x11a, %edx            # imm = 0x11A
callq 0x2140
leaq 0x16ee4(%rip), %rdi     # 0x2ae0e
leaq 0x16d9e(%rip), %rsi     # 0x2accf
leaq 0x16f2a(%rip), %rcx     # 0x2ae62
movl $0xf7, %edx
callq 0x2140
leaq 0x16f0d(%rip), %rdi     # 0x2ae56
leaq 0x16d7f(%rip), %rsi     # 0x2accf
leaq 0x16f0b(%rip), %rcx     # 0x2ae62
movl $0x10e, %edx            # imm = 0x10E
callq 0x2140
leaq 0x16ed5(%rip), %rdi     # 0x2ae3d
leaq 0x16d60(%rip), %rsi     # 0x2accf
leaq 0x16eec(%rip), %rcx     # 0x2ae62
movl $0xfd, %edx
callq 0x2140
jmp 0x13f86
jmp 0x13f8b
jmp 0x13f8b
movq %rax, %rbx
jmp 0x13fc0
movq %rax, %rbx
jmp 0x13fd7
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x13fa9
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x13fc0
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x13fd7
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x13fee
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x14008
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x2310