// File: 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:
subq $0x348, %rsp            # imm = 0x348
movq %rdi, 0xa8(%rsp)
movq %rdi, %rax
movq %rax, 0xb0(%rsp)
movl 0x350(%rsp), %eax
movq %rdi, 0x340(%rsp)
movq %rsi, 0x338(%rsp)
movl %edx, 0x334(%rsp)
movl %ecx, 0x330(%rsp)
movl %r8d, 0x32c(%rsp)
movq %r9, 0x320(%rsp)
movq 0x320(%rsp), %rsi
leaq 0x40092(%rip), %rdi     # 0x4b940
callq 0x138e0
leaq 0x2f0(%rsp), %rdi
callq 0x13f30
movq 0x338(%rsp), %rsi
leaq 0x2d8(%rsp), %rdi
callq 0x96b0
jmp 0xb8d7
movl $0x0, 0x2a4(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x280(%rsp)
movq $0x0, 0x290(%rsp)
leaq 0x280(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x13ba0
movq 0xa0(%rsp), %rdx
leaq 0x2a8(%rsp), %rdi
leaq 0x2a4(%rsp), %rsi
callq 0x13f40
jmp 0xb92d
leaq 0x2f0(%rsp), %rdi
leaq 0x2d8(%rsp), %rsi
callq 0x13f80
movq %rax, 0x98(%rsp)
jmp 0xb94c
movq 0x98(%rsp), %rdi
leaq 0x2a8(%rsp), %rsi
movq %rsi, 0x90(%rsp)
callq 0x14070
movq 0x90(%rsp), %rdi
callq 0x140b0
leaq 0x280(%rsp), %rdi
callq 0x13150
leaq 0x2d8(%rsp), %rdi
callq 0x12350
movl $0xffffffff, 0x27c(%rsp) # imm = 0xFFFFFFFF
leaq 0x260(%rsp), %rdi
callq 0x13ba0
movl 0x334(%rsp), %esi
leaq 0x25c(%rsp), %rdi
callq 0x13bb0
jmp 0xb9be
movl $0x0, 0x258(%rsp)
movl 0x258(%rsp), %eax
movq 0x338(%rsp), %rcx
cmpl 0x40(%rcx), %eax
jge 0xc072
movq 0x338(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x80(%rsp)
leaq 0x25c(%rsp), %rdi
callq 0x13bd0
movl %eax, 0x8c(%rsp)
jmp 0xba0b
movl 0x8c(%rsp), %eax
cltq
movq %rax, 0x70(%rsp)
movq 0x338(%rsp), %rdi
addq $0x8, %rdi
callq 0x13c00
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movq 0x70(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
callq 0x13890
movq %rax, 0x250(%rsp)
movq 0x338(%rsp), %rdi
movq 0x250(%rsp), %rsi
callq 0x7a10
movq %rax, 0x78(%rsp)
jmp 0xba6b
movq 0x78(%rsp), %rax
movq %rax, 0x248(%rsp)
leaq 0x218(%rsp), %rdi
callq 0x140d0
leaq 0x2f0(%rsp), %rax
movq %rax, 0x210(%rsp)
movq 0x210(%rsp), %rdi
callq 0x140e0
movq %rax, 0x208(%rsp)
movq 0x210(%rsp), %rdi
callq 0x14110
movq %rax, 0x200(%rsp)
leaq 0x208(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x14140
testb $0x1, %al
jne 0xbadd
jmp 0xbcb0
leaq 0x208(%rsp), %rdi
callq 0x14160
movq %rax, 0x1f8(%rsp)
movq 0x1f8(%rsp), %rdi
movq 0x250(%rsp), %rdx
leaq 0x248(%rsp), %rsi
xorl %ecx, %ecx
callq 0x6e60
movb %al, 0x6f(%rsp)
jmp 0xbb17
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0xbbcb
jmp 0xbb25
jmp 0xbcb0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xc13c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xbb7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
leaq 0x2a8(%rsp), %rdi
callq 0x140b0
leaq 0x280(%rsp), %rdi
callq 0x13150
leaq 0x2d8(%rsp), %rdi
callq 0x12350
jmp 0xc13c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xc12f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xc060
leaq 0x1e0(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x13ba0
leaq 0x1b0(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x13c20
movq 0x58(%rsp), %r8
movq 0x60(%rsp), %r9
movq 0x1f8(%rsp), %rdi
movq 0x250(%rsp), %rsi
movl 0x350(%rsp), %ecx
movl 0x18(%rdi), %edx
movq %rdi, %r10
addq $0x20, %r10
movq %rsp, %rax
movq %r10, 0x28(%rax)
movl %edx, 0x20(%rax)
movl %ecx, 0x18(%rax)
leaq 0x218(%rsp), %rcx
movq %rcx, (%rax)
movl $0x4, 0x10(%rax)
movl $0x0, 0x8(%rax)
leaq 0x248(%rsp), %rdx
xorl %ecx, %ecx
callq 0x8ff0
jmp 0xbc51
leaq 0x1b0(%rsp), %rdi
callq 0x13e10
leaq 0x1e0(%rsp), %rdi
callq 0x13150
leaq 0x208(%rsp), %rdi
callq 0x14180
jmp 0xbabf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x13e10
leaq 0x1e0(%rsp), %rdi
callq 0x13150
jmp 0xc060
leaq 0x180(%rsp), %rdi
callq 0x13f30
leaq 0x218(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
callq 0x12f30
movq %rax, 0x170(%rsp)
movq 0x178(%rsp), %rdi
callq 0x141b0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x141e0
testb $0x1, %al
jne 0xbd15
jmp 0xc00c
leaq 0x170(%rsp), %rdi
callq 0x14200
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rsi
addq $0x18, %rsi
leaq 0x148(%rsp), %rdi
callq 0x14220
jmp 0xbd45
movq 0x160(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rdi
callq 0x11250
movq %rax, 0x138(%rsp)
movq 0x140(%rsp), %rdi
callq 0x11280
movq %rax, 0x130(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x112b0
testb $0x1, %al
jne 0xbd9a
jmp 0xbe09
leaq 0x138(%rsp), %rdi
callq 0x112f0
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x12800
jmp 0xbdc6
jmp 0xbdc8
leaq 0x138(%rsp), %rdi
callq 0x11300
jmp 0xbd7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xc053
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
jmp 0xbffd
movq 0x160(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0x124(%rsp)
movq 0x160(%rsp), %rax
movl 0x30(%rax), %ecx
movl 0x124(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x124(%rsp)
movq 0x160(%rsp), %rsi
addq $0x40, %rsi
leaq 0x180(%rsp), %rdi
callq 0x14320
movq %rax, 0x50(%rsp)
jmp 0xbe56
movq 0x50(%rsp), %rax
cmpq $0x0, %rax
je 0xbf24
movq 0x160(%rsp), %rsi
addq $0x40, %rsi
leaq 0x180(%rsp), %rdi
callq 0x13f80
movq %rax, 0x48(%rsp)
jmp 0xbe85
movq 0x48(%rsp), %rax
movl (%rax), %eax
cmpl 0x124(%rsp), %eax
jge 0xbf1f
leaq 0x100(%rsp), %rdi
leaq 0x124(%rsp), %rsi
leaq 0x148(%rsp), %rdx
callq 0x14380
jmp 0xbeb8
movq 0x160(%rsp), %rsi
addq $0x40, %rsi
leaq 0x180(%rsp), %rdi
callq 0x13f80
movq %rax, 0x40(%rsp)
jmp 0xbed8
movq 0x40(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x14070
leaq 0x100(%rsp), %rdi
callq 0x140b0
jmp 0xbf1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x140b0
jmp 0xbffd
jmp 0xbfa7
leaq 0xe0(%rsp), %rdi
leaq 0x124(%rsp), %rsi
leaq 0x148(%rsp), %rdx
callq 0x14380
jmp 0xbf43
movq 0x160(%rsp), %rsi
addq $0x40, %rsi
leaq 0x180(%rsp), %rdi
callq 0x13f80
movq %rax, 0x38(%rsp)
jmp 0xbf63
movq 0x38(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x14070
leaq 0xe0(%rsp), %rdi
callq 0x140b0
jmp 0xbfa7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x140b0
jmp 0xbffd
movl 0x124(%rsp), %eax
cmpl 0x27c(%rsp), %eax
jle 0xbfde
movl 0x124(%rsp), %eax
movl %eax, 0x27c(%rsp)
leaq 0x260(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x128e0
jmp 0xbfdc
jmp 0xbfde
leaq 0x148(%rsp), %rdi
callq 0x13150
leaq 0x170(%rsp), %rdi
callq 0x143c0
jmp 0xbcf7
leaq 0x148(%rsp), %rdi
callq 0x13150
jmp 0xc053
leaq 0x2f0(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x143f0
jmp 0xc023
leaq 0x180(%rsp), %rdi
callq 0x14420
leaq 0x218(%rsp), %rdi
callq 0x14430
movl 0x258(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x258(%rsp)
jmp 0xb9c9
leaq 0x180(%rsp), %rdi
callq 0x14420
leaq 0x218(%rsp), %rdi
callq 0x14430
jmp 0xc12f
leaq 0xc0(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x8820
jmp 0xc089
movl 0x27c(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x9b40
movl %eax, 0x34(%rsp)
jmp 0xc0a7
movq 0xa8(%rsp), %rdi
movl 0x34(%rsp), %ecx
movl 0x30(%rsp), %eax
addl %ecx, %eax
movl %eax, 0xbc(%rsp)
leaq 0xc0(%rsp), %rsi
leaq 0xbc(%rsp), %rdx
callq 0x13ef0
jmp 0xc0d7
leaq 0xc0(%rsp), %rdi
callq 0x4700
leaq 0x260(%rsp), %rdi
callq 0x13150
leaq 0x2f0(%rsp), %rdi
callq 0x14420
movq 0xb0(%rsp), %rax
addq $0x348, %rsp            # imm = 0x348
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2d0(%rsp)
movl %eax, 0x2cc(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4700
leaq 0x260(%rsp), %rdi
callq 0x13150
leaq 0x2f0(%rsp), %rdi
callq 0x14420
movq 0x2d0(%rsp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)