// File: /workspace/github_cpp_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x16d8b(%rip), %rdi     # 0x1d840
movq %r9, %rsi
callq 0x9e2a
leaq 0x108(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x5a80
movq %rbx, 0xc8(%rsp)
leaq 0x98(%rsp), %r12
andl $0x0, -0x8(%r12)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x9f12
leaq 0x90(%rsp), %rsi
movq %rax, %rdi
callq 0x9f78
movq %r12, %rdi
callq 0xd7a4
leaq 0x30(%rsp), %rdi
callq 0xd7a4
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x9958
leaq 0xe0(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
pushq $-0x1
popq %r13
xorl %eax, %eax
leaq 0x130(%rsp), %rbp
movq %r14, 0xd0(%rsp)
cmpl 0x40(%r14), %eax
jge 0x6e53
movq %rax, 0xd8(%rsp)
imull $0x41c64e6d, %r15d, %eax # imm = 0x41C64E6D
shrl $0x10, %r15d
movl %r15d, %ecx
andl $0x7fff, %ecx           # imm = 0x7FFF
addl $0x3039, %eax           # imm = 0x3039
movl %eax, 0x5c(%rsp)
movq 0x8(%r14), %rdi
movq 0x10(%r14), %rax
subq %rdi, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
movq %rax, %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
imulq $0x28, %rdx, %r12
addq %rdi, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x4d3c
andl $0x0, 0x98(%rsp)
andq $0x0, 0xa0(%rsp)
movq %rax, 0xc0(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
andq $0x0, 0xb8(%rsp)
movq 0x118(%rsp), %r15
leaq 0x108(%rsp), %rax
cmpq %rax, %r15
je 0x6cf4
leaq 0x20(%r15), %r14
movq %r14, %rdi
leaq 0xc0(%rsp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x481a
testb %al, %al
je 0x6cf4
andq $0x0, 0x40(%rsp)
andl $0x0, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
andq $0x0, 0x88(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movl 0x38(%r15), %eax
leaq 0x40(%r15), %rcx
movq %rcx, 0x28(%rsp)
movl %eax, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
movl 0x180(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
leaq 0xc0(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r14
movq %r14, %r8
movq %rbx, %r9
callq 0x56f5
movq %rbx, %rdi
callq 0xcbbc
movq %r14, %rdi
callq 0xd7a4
movq %r15, %rdi
callq 0x3200
movq %rax, %r15
jmp 0x6c21
andl $0x0, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %rax, 0x80(%rsp)
andq $0x0, 0x88(%rsp)
movq 0xa8(%rsp), %r12
leaq 0x98(%rsp), %rax
cmpq %rax, %r12
je 0x6e12
leaq 0x38(%r12), %rsi
movq %rbp, %rdi
callq 0x9f92
movq 0x20(%r12), %r14
movq 0x28(%r12), %r15
cmpq %r15, %r14
je 0x6d60
movq %rbp, %rdi
movq %r14, %rsi
callq 0x99be
addq $0x4, %r14
jmp 0x6d4a
movl 0x50(%r12), %r14d
addl 0x54(%r12), %r14d
leaq 0x60(%r12), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9fe0
testq %rax, %rax
je 0x6dae
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9f12
cmpl %r14d, (%rax)
jge 0x6de2
movl %r14d, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x9f92
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9f12
jmp 0x6dcb
movl %r14d, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x9f92
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9f12
movq %rax, %rdi
leaq 0x30(%rsp), %rsi
callq 0x9f78
leaq 0x38(%rsp), %rdi
callq 0xd7a4
cmpl %r13d, %r14d
jle 0x6dfa
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x99dc
movl %r14d, %r13d
movq %rbp, %rdi
callq 0xd7a4
movq %r12, %rdi
callq 0x3340
movq %rax, %r12
jmp 0x6d22
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
callq 0xcbd0
movq 0xd0(%rsp), %r14
movq %rbx, %rdi
callq 0xcfb8
leaq 0x90(%rsp), %rdi
callq 0xcfcc
movq 0xd8(%rsp), %rax
incl %eax
movl 0x5c(%rsp), %r15d
jmp 0x6b80
leaq 0x90(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x52f3
leaq 0x90(%rsp), %rdi
callq 0x5c2e
movq 0xc8(%rsp), %r14
movl %eax, %ebx
leaq 0x90(%rsp), %rsi
movq %r14, %rdi
callq 0x33a0
addl %r13d, %ebx
movl %ebx, 0x20(%r14)
leaq 0x90(%rsp), %rdi
callq 0x3550
leaq 0xe0(%rsp), %rdi
callq 0xd7a4
leaq 0x100(%rsp), %rdi
callq 0xcfb8
movq %r14, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6f7b
movq %rax, %rbx
movq %r12, %rdi
callq 0xd7a4
leaq 0x30(%rsp), %rdi
callq 0xd7a4
leaq 0x60(%rsp), %rdi
callq 0x9958
jmp 0x6f88
movq %rax, %rbx
jmp 0x6f88
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x3550
jmp 0x6f7b
jmp 0x6f4d
jmp 0x6f1e
jmp 0x6f54
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xd7a4
jmp 0x6f57
jmp 0x6f54
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xcbbc
leaq 0x30(%rsp), %rdi
callq 0xd7a4
jmp 0x6f6e
movq %rax, %rbx
jmp 0x6f6e
movq %rax, %rbx
jmp 0x6f64
jmp 0x6f54
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0xd7a4
leaq 0x60(%rsp), %rdi
callq 0xcfb8
leaq 0x90(%rsp), %rdi
callq 0xcfcc
leaq 0xe0(%rsp), %rdi
callq 0xd7a4
leaq 0x100(%rsp), %rdi
callq 0xcfb8
movq %rbx, %rdi
callq 0x34a0