// File: /workspace/github_cpp_makefile_star2_O3/tanakh[P]icfpc2015/main.cpp
// Function: beam_search[abi:cxx11](problem const&, int, int, int, std::vector<double, std::allocator<double>> const&, int)
pair<string, int> beam_search(const problem &prob, int seed, int tle, int mle,
                              const vector<double> &param, int width)
{
  eparam = param;

  map<vector<vector<int>>, pair<int,vector<int>>> beam;
  {
    auto bd = make_board(prob);
    beam[bd] = make_pair(0, vector<int>());
  }

  int best_score = -1;
  vector<int> best_move;

  rng r(seed);
  for (int turn = 0; turn < prob.source_length; turn++) {
    const unit &u = prob.units[r.get() % prob.units.size()];
    pt pos = get_init_pos(prob, u);

    multiset<candidate> bests;
    for (auto &cur_bd: beam) {
      if (!check(cur_bd.first, pos, u, 0)) break;

      vector<int> hist;
      set<pair<pair<int,int>, int>> ss;

      rec_beam(cur_bd.first, u, pos, 0, hist, ss, bests, 0, CW, width,
               cur_bd.second.first, cur_bd.second.second);
    }

    map<vector<vector<int>>, pair<int,vector<int>>> next_beam;
    // cerr << "BESTS: size = " << bests.size() << endl;
    for (auto &it: bests) {
      // cerr << "BESTS: " << it.score << ", " << to_ans(it.moves) << endl;

      auto moves = it.prev_moves;
      for (auto &m: it.moves)
        moves.push_back(m);
      auto move_score = it.prev_score;
      move_score += it.move_score;
      if (next_beam.count(it.board)) {
        if (next_beam[it.board].first < move_score)
          next_beam[it.board] = make_pair(move_score, moves);
      }
      else
        next_beam[it.board] = make_pair(move_score, moves);

      if (move_score > best_score) {
        // cerr << "*** " << best_score << endl;
        best_score = move_score;
        best_move = moves;
      }
    }
    beam = next_beam;
  }

  auto moves = to_ans(best_move);
  return make_pair(moves, best_score + power_score(moves));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x16c1e(%rip), %rdi     # 0x1e830
movq %r9, %rsi
callq 0xc2d0
leaq 0x110(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x90(%rsp), %rdi
movq %r14, 0xc0(%rsp)
movq %r14, %rsi
callq 0x6611
leaq 0x108(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xc3ac
movl $0x0, (%rax)
movq 0x8(%rax), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
testq %rdi, %rdi
je 0x7c8a
callq 0x32f0
movq %rbx, 0xe8(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xbbae
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq $0x0, 0xe0(%rsp)
movq 0xc0(%rsp), %rdi
cmpl $0x0, 0x40(%rdi)
jle 0x8049
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x60(%rsp), %rbx
leaq 0xf0(%rsp), %r14
xorl %r15d, %r15d
movl %r15d, 0x5c(%rsp)
movl %ebp, 0x58(%rsp)
movl %ebp, %eax
shrl $0x10, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq 0x8(%rdi), %rcx
movq 0x10(%rdi), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rsi
xorl %edx, %edx
divq %rsi
leaq (%rdx,%rdx,4), %rax
movl %eax, %eax
leaq (%rcx,%rax,8), %r15
movq %r15, %rsi
callq 0x5805
movq %rax, 0xc8(%rsp)
movl $0x0, 0x98(%rsp)
xorl %ecx, %ecx
movq %rcx, 0xa0(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movq %rcx, 0xb8(%rsp)
movq 0x120(%rsp), %r13
leaq 0x110(%rsp), %rax
cmpq %rax, %r13
je 0x7fcf
leaq 0x20(%r13), %rbp
movq %rbp, %rdi
leaq 0xc8(%rsp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x504f
testb %al, %al
je 0x7e4d
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
xorl %edx, %edx
movq %rdx, 0x40(%rsp)
movl %edx, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq %rdx, 0x88(%rsp)
movl 0x38(%r13), %eax
leaq 0x40(%r13), %rcx
movq %rcx, 0x28(%rsp)
movl %eax, 0x20(%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %edx, 0x8(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
leaq 0xc8(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
movq %rbx, %rbp
movq %rbx, %r9
callq 0x628e
movq %rbp, %rdi
callq 0xf560
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7e31
callq 0x32f0
movq %r13, %rdi
callq 0x3210
movq %rax, %r13
leaq 0x110(%rsp), %rax
cmpq %rax, %r13
jne 0x7d7b
movq 0xa8(%rsp), %r15
movl $0x0, 0x68(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq %rcx, 0x88(%rsp)
leaq 0x98(%rsp), %rax
cmpq %rax, %r15
je 0x7ff8
leaq 0x38(%r15), %rsi
movq %r14, %rdi
callq 0xc426
movq 0x20(%r15), %r13
movq 0x28(%r15), %rbp
cmpq %rbp, %r13
je 0x7edf
movq 0xf8(%rsp), %rsi
cmpq 0x100(%rsp), %rsi
je 0x7ece
movl (%r13), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xf8(%rsp)
jmp 0x7ed9
movq %r14, %rdi
movq %r13, %rdx
callq 0x10aaa
addq $0x4, %r13
jmp 0x7ea3
movl 0x50(%r15), %ebp
addl 0x54(%r15), %ebp
leaq 0x60(%r15), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x121f0
leaq 0x68(%rsp), %rcx
cmpq %rcx, %rax
je 0x7f31
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc3ac
cmpl %ebp, (%rax)
jge 0x7f95
movl %ebp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0xc426
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc3ac
jmp 0x7f4d
movl %ebp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0xc426
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc3ac
movl 0x30(%rsp), %ecx
movl %ecx, (%rax)
movq 0x8(%rax), %rdi
movups 0x38(%rsp), %xmm0
movups %xmm0, 0x8(%rax)
movq 0x48(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
je 0x7f95
callq 0x32f0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x7f95
callq 0x32f0
cmpl %r12d, %ebp
jle 0x7fad
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
callq 0xbc10
movl %ebp, %r12d
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x7fbf
callq 0x32f0
movq %r15, %rdi
callq 0x3360
movq %rax, %r15
jmp 0x7e7e
movl $0x0, 0x68(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq %rcx, 0x88(%rsp)
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0xf574
movl 0x5c(%rsp), %r15d
movl 0x58(%rsp), %ebp
imull $0x41c64e6d, %ebp, %ebp # imm = 0x41C64E6D
addl $0x3039, %ebp           # imm = 0x3039
movq %rbx, %rdi
callq 0xf91e
leaq 0x90(%rsp), %rdi
callq 0xf932
incl %r15d
movq 0xc0(%rsp), %rdi
cmpl 0x40(%rdi), %r15d
jl 0x7cde
jmp 0x804f
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x90(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x5df8
leaq 0x90(%rsp), %rdi
callq 0x6821
movq 0xe8(%rsp), %r14
movl %eax, %ebx
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeed8
addl %r12d, %ebx
movl %ebx, 0x20(%r14)
leaq 0xa0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x80ba
callq 0x32f0
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x80cc
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0xf91e
movq %r14, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x81a0
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xbbae
jmp 0x81b2
movq %rax, %rbx
jmp 0x81b2
movq %rax, %rbx
leaq 0xa0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x81a0
callq 0x32f0
jmp 0x81a0
jmp 0x816d
jmp 0x8134
jmp 0x8174
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x8177
callq 0x32f0
jmp 0x8177
jmp 0x8174
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xf560
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x8193
callq 0x32f0
jmp 0x8193
movq %rax, %rbx
jmp 0x8193
movq %rax, %rbx
jmp 0x8189
jmp 0x8174
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x8189
callq 0x32f0
leaq 0x60(%rsp), %rdi
callq 0xf91e
leaq 0x90(%rsp), %rdi
callq 0xf932
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x81b2
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0xf91e
movq %rbx, %rdi
callq 0x34a0