// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %rbx
leaq 0x16beb(%rip), %rdi     # 0x1e840
movq %r9, %rsi
callq 0xc260
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 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x666c
leaq 0x108(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc32e
movl $0x0, (%rax)
movq 0x8(%rax), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
testq %rdi, %rdi
je 0x7cbf
callq 0x32f0
movq %rbx, 0xe0(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xbbf8
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movq $0x0, 0xd0(%rsp)
cmpl $0x0, 0x40(%r15)
jle 0x8074
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0xf0(%rsp), %r13
xorl %r14d, %r14d
movq %r15, 0xe8(%rsp)
movl %r14d, 0x54(%rsp)
movl %ebp, 0x50(%rsp)
movl %ebp, %eax
shrl $0x10, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movq 0x8(%r15), %rcx
movq 0x10(%r15), %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), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x5816
movq %rax, 0xb8(%rsp)
movl $0x0, 0x60(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x68(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq %rcx, 0x80(%rsp)
movq 0x120(%rsp), %r15
leaq 0x110(%rsp), %rax
cmpq %rax, %r15
je 0x7e77
leaq 0x20(%r15), %r12
movq %r12, %rdi
leaq 0xb8(%rsp), %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x5062
movl %eax, %ebp
testb %al, %al
je 0x7e77
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
xorl %edx, %edx
movq %rdx, 0x40(%rsp)
movl %edx, 0x90(%rsp)
movq %rdx, 0x98(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq %rax, 0xa8(%rsp)
movq %rdx, 0xb0(%rsp)
movl 0x38(%r15), %eax
leaq 0x40(%r15), %rcx
movq %rcx, 0x28(%rsp)
movl %eax, 0x20(%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %edx, 0x8(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0xb8(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0x88(%rsp), %r12
movq %r12, %r9
callq 0x62e5
movq %r12, %rdi
callq 0xf43c
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7e62
callq 0x32f0
testb %bpl, %bpl
je 0x7e77
movq %r15, %rdi
callq 0x3210
movq %rax, %r15
jmp 0x7d8b
movl $0x0, 0x90(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x98(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq %rax, 0xa8(%rsp)
movq %rcx, 0xb0(%rsp)
movq 0x70(%rsp), %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
leaq 0x88(%rsp), %r12
je 0x8026
leaq 0x38(%r14), %rsi
movq %r13, %rdi
callq 0xc3a8
movq 0x20(%r14), %r15
movq 0x28(%r14), %r12
cmpq %r12, %r15
je 0x7f16
movq 0xf8(%rsp), %rsi
cmpq 0x100(%rsp), %rsi
je 0x7f05
movl (%r15), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xf8(%rsp)
jmp 0x7f10
movq %r13, %rdi
movq %r15, %rdx
callq 0x1098a
addq $0x4, %r15
jmp 0x7edb
movl 0x50(%r14), %ebp
addl 0x54(%r14), %ebp
leaq 0x60(%r14), %r15
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1213c
leaq 0x90(%rsp), %rcx
cmpq %rcx, %rax
je 0x7f73
movq %r12, %rdi
movq %r15, %rsi
callq 0xc32e
cmpl %ebp, (%rax)
jge 0x7fe5
movl %ebp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0xc3a8
movq %r12, %rdi
movq %r15, %rsi
callq 0xc32e
jmp 0x7f8f
movl %ebp, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0xc3a8
movq %r12, %rdi
movq %r15, %rsi
callq 0xc32e
movl 0x30(%rsp), %ecx
movl %ecx, (%rax)
movq 0x8(%rax), %rdi
movups 0x38(%rsp), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm0, 0x8(%rax)
movq 0x48(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rdi, 0x38(%rsp)
movups %xmm1, 0x40(%rsp)
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
je 0x7fd6
callq 0x32f0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x7fe5
callq 0x32f0
cmpl %ebx, %ebp
jle 0x7ffb
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0xbc54
movl %ebp, %ebx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x800d
callq 0x32f0
movq %r14, %rdi
callq 0x3360
movq %rax, %r14
leaq 0x60(%rsp), %rax
cmpq %rax, %r14
jne 0x7ec7
leaq 0x108(%rsp), %rdi
movq %r12, %rsi
callq 0xf450
movl 0x54(%rsp), %r14d
movl 0x50(%rsp), %ebp
imull $0x41c64e6d, %ebp, %ebp # imm = 0x41C64E6D
addl $0x3039, %ebp           # imm = 0x3039
movq %r12, %rdi
callq 0xf7fc
leaq 0x58(%rsp), %rdi
callq 0xf810
incl %r14d
movq 0xe8(%rsp), %r15
cmpl 0x40(%r15), %r14d
jl 0x7d0b
jmp 0x8079
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x58(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x5e03
leaq 0x58(%rsp), %rdi
callq 0x686e
movq 0xe0(%rsp), %r14
movl %eax, %ebp
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeda4
addl %ebx, %ebp
movl %ebp, 0x20(%r14)
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x80d4
callq 0x32f0
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x80e6
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0xf7fc
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 0x81b7
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xbbf8
jmp 0x81c9
movq %rax, %rbx
jmp 0x81c9
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x81b7
callq 0x32f0
jmp 0x81b7
jmp 0x8184
jmp 0x816e
jmp 0x818b
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0xf43c
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x81ad
callq 0x32f0
jmp 0x81ad
movq %rax, %rbx
jmp 0x81ad
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x818e
callq 0x32f0
jmp 0x818e
jmp 0x818b
movq %rax, %rbx
jmp 0x81a0
jmp 0x818b
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x81a0
callq 0x32f0
leaq 0x88(%rsp), %rdi
callq 0xf7fc
leaq 0x58(%rsp), %rdi
callq 0xf810
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x81c9
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0xf7fc
movq %rbx, %rdi
callq 0x34b0