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

  auto bd = make_board(prob);
  if (progress) print_board(bd);

  int move_score = 0;
  vector<int> moves;

  int ls_old = 0;

  int word_used = 0;

  auto words = get_words();

  rng r(seed);
  vector<int> unit_ids;
  for (int i = 0; i < prob.source_length; i++)
    unit_ids.push_back(r.get() % prob.units.size());

  set<pair<pair<int,int>, int>> current_poss;
  int turn = 0;
  pt pos = get_init_pos(prob, prob.units[unit_ids[turn]]);
  int rot = 0;

  if (!check(bd, pos, prob.units[unit_ids[turn]], rot))
    return make_pair("", 0);

  for (; turn < (int)unit_ids.size(); ) {

    bool injected = false;

    for (int j = 0; j < (int)words.size(); j++) {
      if (words[j].length() > 20) continue;

      if (word_used & (1 << j)) continue;

      // if (genrand_int31() < 0.5) continue;

      int tmp_turn = turn;
      pt tmp_pos = pos;
      int tmp_rot = rot;
      vector<vector<int>> tmp_bd = bd;
      set<pair<pair<int,int>, int>> tmp_prevs = current_poss;

      // cerr << "*** << " << tmp_bd.size() << ", " << bd.size() << ", " << words[j] << endl;

      auto sc =
        can_use(prob, words[j], prob.units, unit_ids, tmp_turn, tmp_pos, tmp_rot, tmp_bd, tmp_prevs);

      if (sc >= 0) {
        // cerr << "FOUND: " << words[j] << endl;
        // print_board(tmp_bd);

        word_used |= (1 << j);

        for (auto &ch: words[j])
          moves.push_back(to_command(ch));
        move_score += sc;

        // update
        turn = tmp_turn;
        pos = tmp_pos;
        rot = tmp_rot;
        bd = tmp_bd;
        current_poss = tmp_prevs;

        injected = true;
        break;
      }
    }

    if (injected) continue;

    vector<int> hist;
    candidate best;
    // current_poss.erase(make_pair(pt2pair(pos), rot));
    rec(bd, prob.units[unit_ids[turn]], pos, rot, hist, current_poss, best, ls_old, CW);

    bd = best.board;
    for (auto &m: best.moves)
      moves.push_back(m);
    move_score += best.move_score;
    ls_old = best.ls_old;

    turn++;
    current_poss.clear();
    rot = 0;
    if (turn < (int)unit_ids.size())
      pos = get_init_pos(prob, prob.units[unit_ids[turn]]);
  }

  auto ms = to_ans(moves);
  return make_pair(ms, move_score + power_score(ms));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r13
movb 0x1b0(%rsp), %bpl
leaq 0x173bb(%rip), %rdi     # 0x1d840
movq %r9, %rsi
callq 0x9e2a
leaq 0x98(%rsp), %rdi
movq %r14, 0x30(%rsp)
movq %r14, %rsi
callq 0x5a80
testb %bpl, %bpl
je 0x64b4
leaq 0x98(%rsp), %rdi
callq 0x4a6c
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
andq $0x0, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x5b5f
leaq 0x60(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq 0x30(%rsp), %rbp
leaq 0x8(%rbp), %rax
movq %rax, 0x80(%rsp)
xorl %r15d, %r15d
pushq $0x28
popq %r12
leaq 0xe0(%rsp), %rsi
cmpl 0x40(%rbp), %r15d
jge 0x654f
movl %ebx, %ecx
shrl $0x10, %ecx
andl $0x7fff, %ecx           # imm = 0x7FFF
movq 0x10(%rbp), %rax
subq 0x8(%rbp), %rax
cqto
idivq %r12
movq %rax, %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
movl %edx, 0xe0(%rsp)
movq %r14, %rdi
callq 0xd9b4
imull $0x41c64e6d, %ebx, %ebx # imm = 0x41C64E6D
addl $0x3039, %ebx           # imm = 0x3039
incl %r15d
jmp 0x64fe
leaq 0x150(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x60(%rsp), %rbx
movslq (%rbx), %rax
movq 0x8(%rbp), %r14
imulq $0x28, %rax, %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0x4d3c
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
movslq (%rbx), %rax
imulq $0x28, %rax, %rdx
addq %r14, %rdx
leaq 0x98(%rsp), %rdi
xorl %ecx, %ecx
callq 0x481a
testb %al, %al
je 0x691c
movq %r13, 0x140(%rsp)
movq $0x0, 0x10(%rsp)
pushq $0x1
popq %rdx
leaq 0x98(%rsp), %r14
xorl %r13d, %r13d
movq $0x0, 0x78(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
shrq $0x2, %rax
cmpl %eax, 0x10(%rsp)
jge 0x693c
xorl %ebp, %ebp
movq $0x0, 0x88(%rsp)
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rcx
subq %rax, %rcx
shrq $0x5, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jge 0x67ab
movq %rbp, %r12
shlq $0x5, %r12
cmpq $0x14, 0x8(%rax,%r12)
ja 0x67a3
movl %edx, %r15d
movl %ebp, %ecx
shll %cl, %r15d
btl %ebp, %r13d
jb 0x67a3
movq 0x10(%rsp), %rax
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x90(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x98fe
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x148(%rsp), %rsi
callq 0xc73c
movq 0xc8(%rsp), %rsi
addq %r12, %rsi
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq 0x88(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x2c(%rsp), %r8
leaq 0x98(%rsp), %r9
pushq %rbx
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x61a5
addq $0x20, %rsp
movl %eax, %r14d
testl %eax, %eax
js 0x677c
orl %r15d, %r13d
movq 0xc8(%rsp), %rax
movq (%rax,%r12), %r15
movq 0x8(%rax,%r12), %r12
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x6728
movsbl (%r15,%rbx), %edi
callq 0x44ae
movl %eax, 0x3c(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x3c(%rsp), %rsi
callq 0xd9b4
incq %rbx
jmp 0x66fe
movl 0x24(%rsp), %eax
movq %rax, 0x10(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x9a9e
leaq 0x148(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xc94c
addl %r14d, 0xc(%rsp)
movb $0x1, %al
movq %rax, 0x88(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xcbbc
leaq 0x40(%rsp), %rdi
callq 0x9958
testl %r14d, %r14d
pushq $0x1
popq %rdx
leaq 0x98(%rsp), %r14
jns 0x67ab
incq %rbp
jmp 0x660e
testb $0x1, 0x88(%rsp)
movq 0x30(%rsp), %r15
leaq 0x120(%rsp), %rbx
jne 0x65e8
movl %edx, %ebp
andq $0x0, 0x50(%rsp)
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movups %xmm0, (%rbx)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movabsq $-0x3dfd5fa0e0000000, %rax # imm = 0xC202A05F20000000
movq %rax, 0x138(%rsp)
movslq 0x10(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
imulq $0x28, %rax, %rsi
movq 0x80(%rsp), %rax
addq (%rax), %rsi
subq $0x10, %rsp
movq %r14, %rdi
leaq 0x38(%rsp), %rdx
movl 0x2c(%rsp), %ecx
leaq 0x50(%rsp), %r8
leaq 0x158(%rsp), %r9
pushq 0x88(%rsp)
leaq 0xf8(%rsp), %rax
pushq %rax
callq 0x5412
addq $0x20, %rsp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9a9e
movq %r14, %r12
movq 0xe0(%rsp), %r14
movq 0xe8(%rsp), %rbx
cmpq %rbx, %r14
je 0x6894
leaq 0xb0(%rsp), %rdi
movq %r14, %rsi
callq 0x99be
addq $0x4, %r14
jmp 0x6879
movl 0xc(%rsp), %eax
addl 0x110(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x118(%rsp), %eax
movq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rbx
incl %ebx
leaq 0x148(%rsp), %rdi
callq 0xf440
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movq %rbx, 0x10(%rsp)
cmpl %ecx, %ebx
jge 0x68fb
movslq 0x10(%rsp), %rcx
movslq (%rax,%rcx,4), %rax
imulq $0x28, %rax, %rsi
addq 0x8(%r15), %rsi
movq %r15, %rdi
callq 0x4d3c
movq %rax, 0x28(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x9bb4
leaq 0x40(%rsp), %rdi
callq 0xd7a4
movl %ebp, %edx
movq %r12, %r14
jmp 0x65e0
leaq 0xbd87(%rip), %rax      # 0x126aa
leaq 0xe0(%rsp), %rsi
movq %rax, (%rsi)
andl $0x0, 0x8(%rsi)
movq %r13, %rdi
callq 0x9eec
jmp 0x698d
leaq 0xe0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x52f3
leaq 0xe0(%rsp), %rdi
callq 0x5c2e
movq 0x140(%rsp), %r13
movl %eax, %ebx
leaq 0xe0(%rsp), %rsi
movq %r13, %rdi
callq 0x33a0
addl 0xc(%rsp), %ebx
movl %ebx, 0x20(%r13)
leaq 0xe0(%rsp), %rdi
callq 0x3550
leaq 0x148(%rsp), %rdi
callq 0xcbbc
leaq 0x60(%rsp), %rdi
callq 0xd7a4
leaq 0xc8(%rsp), %rdi
callq 0x9d00
leaq 0xb0(%rsp), %rdi
callq 0xd7a4
leaq 0x98(%rsp), %rdi
callq 0x9958
movq %r13, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6a2d
jmp 0x6a2d
movq %rax, %rbx
jmp 0x6a7f
jmp 0x6a07
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x3550
jmp 0x6a4e
jmp 0x6a2d
movq %rax, %rbx
jmp 0x6a72
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x9bb4
leaq 0x40(%rsp), %rdi
callq 0xd7a4
jmp 0x6a4e
movq %rax, %rbx
jmp 0x6a5b
movq %rax, %rbx
jmp 0x6a44
movq %rax, %rbx
jmp 0x6a4e
jmp 0x6a34
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xcbbc
leaq 0x40(%rsp), %rdi
callq 0x9958
leaq 0x148(%rsp), %rdi
callq 0xcbbc
leaq 0x60(%rsp), %rdi
callq 0xd7a4
leaq 0xc8(%rsp), %rdi
callq 0x9d00
leaq 0xb0(%rsp), %rdi
callq 0xd7a4
leaq 0x98(%rsp), %rdi
callq 0x9958
movq %rbx, %rdi
callq 0x34a0