// File: tanakh[P]icfpc2015/main.cpp
// Function: annealing(problem const&, int, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, double, double, int, int)
pair<string, int> annealing(const problem &p, int seed, int tle, int mle, const string &tag,
                            double init_temp, double temp_decay, int rounds, int beam_width)
{
  int best_score = -1;
  string best_move;

  vector<double> pp(NUM_FEATURES);
  for (int i = 0; i < (int)pp.size(); i++)
    pp[i] = genrand_real1() - 0.5;
  double score = -1;

  double temp = init_temp;
  for (int t = 0; t < rounds; t++, temp *= temp_decay) {
    if (t % 10 == 0)
      cerr << "round: [" << t << "/" << rounds << "]: "
           << temp << ", " << score << "        \r" << flush;

    if (temp < 1){
      cerr << "REBOOT                                   " << endl;
      temp = best_score / 10;
    }

    auto bkup = pp;

    if (genrand_int31() % 2 == 0) {
      pp[genrand_int31() % pp.size()] = genrand_real1() - 0.5;
    }
    else {
      pp[genrand_int31() % pp.size()] += genrand_real1() * 0.5 - 0.25;
      // pp[genrand_int31() % pp.size()] = genrand_real1() - 0.5;
    }

    // auto rr = solve(p, seed, tle, mle, pp, false);
    auto rr =
      beam_width == 1 ? solve(p, seed, tle, mle, pp, false)
      : beam_search(p, seed, tle, mle, pp, beam_width);

    if (score <= rr.second ||
        genrand_real2() <= exp((rr.second - score) / temp)) {
      score = rr.second;

      if (best_score < rr.second) {
        best_score = rr.second;
        best_move = rr.first;

        cerr << "*BEST* " << p.id << "-" << seed << ", round " << t << ": " << best_score
             << "                      " << endl;
        // cerr << "param[] = [";
        // for (int i = 0; i < (int)pp.size(); i++)
        //   cerr << pp[i] << ", ";
        // cerr << "]" << endl;;

        output_solution(p.id, seed, best_score, best_move, tag);
      }
    }
    else {
      pp = bkup;
    }
  }

  return make_pair(best_move, best_score);
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0xd8(%rsp)
movq %rdi, %rax
movq %rax, 0xe0(%rsp)
movl 0x1e8(%rsp), %eax
movl 0x1e0(%rsp), %eax
movq %rdi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movl %edx, 0x1c4(%rsp)
movl %ecx, 0x1c0(%rsp)
movl %r8d, 0x1bc(%rsp)
movq %r9, 0x1b0(%rsp)
movsd %xmm0, 0x1a8(%rsp)
movsd %xmm1, 0x1a0(%rsp)
movl $0xffffffff, 0x19c(%rsp) # imm = 0xFFFFFFFF
leaq 0x178(%rsp), %rdi
callq 0x4500
leaq 0x15f(%rsp), %rdi
movq %rdi, 0xe8(%rsp)
callq 0x14440
movq 0xe8(%rsp), %rdx
leaq 0x160(%rsp), %rdi
movl $0xa, %esi
callq 0x14450
jmp 0xc211
leaq 0x15f(%rsp), %rdi
callq 0x144d0
movl $0x0, 0x148(%rsp)
movl 0x148(%rsp), %eax
movl %eax, 0xd4(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x144e0
movq %rax, %rcx
movl 0xd4(%rsp), %eax
cmpl %ecx, %eax
jge 0xc2c7
callq 0x4ee0
movsd 0x29db9(%rip), %xmm1    # 0x36018
subsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movslq 0x148(%rsp), %rsi
leaq 0x160(%rsp), %rdi
callq 0x125a0
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x148(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x148(%rsp)
jmp 0xc229
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x15f(%rsp), %rdi
callq 0x144d0
jmp 0xc8fa
movsd 0x29d61(%rip), %xmm0    # 0x36030
movsd %xmm0, 0x140(%rsp)
movsd 0x1a8(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x1e0(%rsp), %eax
jge 0xc8a4
movl 0x134(%rsp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xc463
movq 0x3dcc0(%rip), %rdi     # 0x49fe8
leaq 0x2a2c3(%rip), %rsi     # 0x365f2
callq 0x43c0
movq %rax, 0xc0(%rsp)
jmp 0xc33e
movq 0xc0(%rsp), %rdi
movl 0x134(%rsp), %esi
callq 0x4600
movq %rax, 0xb8(%rsp)
jmp 0xc35c
movq 0xb8(%rsp), %rdi
leaq 0x2a54c(%rip), %rsi     # 0x368b7
callq 0x43c0
movq %rax, 0xb0(%rsp)
jmp 0xc37a
movq 0xb0(%rsp), %rdi
movl 0x1e0(%rsp), %esi
callq 0x4600
movq %rax, 0xa8(%rsp)
jmp 0xc398
movq 0xa8(%rsp), %rdi
leaq 0x2a254(%rip), %rsi     # 0x365fb
callq 0x43c0
movq %rax, 0xa0(%rsp)
jmp 0xc3b6
movq 0xa0(%rsp), %rdi
movsd 0x138(%rsp), %xmm0
callq 0x46a0
movq %rax, 0x98(%rsp)
jmp 0xc3d6
movq 0x98(%rsp), %rdi
leaq 0x2a02a(%rip), %rsi     # 0x3640f
callq 0x43c0
movq %rax, 0x90(%rsp)
jmp 0xc3f4
movq 0x90(%rsp), %rdi
movsd 0x140(%rsp), %xmm0
callq 0x46a0
movq %rax, 0x88(%rsp)
jmp 0xc414
movq 0x88(%rsp), %rdi
leaq 0x2a1dc(%rip), %rsi     # 0x365ff
callq 0x43c0
movq %rax, 0x80(%rsp)
jmp 0xc432
movq 0x80(%rsp), %rdi
movq 0x3db87(%rip), %rsi     # 0x49fc8
callq 0x4400
jmp 0xc448
jmp 0xc463
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xc8ed
movsd 0x29bcd(%rip), %xmm0    # 0x36038
ucomisd 0x138(%rsp), %xmm0
jbe 0xc4bf
movq 0x3db6b(%rip), %rdi     # 0x49fe8
leaq 0x2a185(%rip), %rsi     # 0x36609
callq 0x43c0
movq %rax, 0x78(%rsp)
jmp 0xc490
movq 0x78(%rsp), %rdi
movq 0x3daf4(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xc4a3
movl 0x19c(%rsp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x138(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x14500
jmp 0xc4d6
callq 0x4ed0
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0xc542
callq 0x4ee0
movsd 0x29b20(%rip), %xmm1    # 0x36018
subsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
callq 0x4ed0
movq %rax, 0x68(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x144e0
movq %rax, %rcx
movq 0x68(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
leaq 0x160(%rsp), %rdi
callq 0x125a0
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0xc5a7
callq 0x4ee0
movsd 0x29ac9(%rip), %xmm1    # 0x36018
mulsd %xmm1, %xmm0
movsd 0x29aed(%rip), %xmm1    # 0x36048
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
callq 0x4ed0
movq %rax, 0x58(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x144e0
movq %rax, %rcx
movq 0x58(%rsp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rsi
leaq 0x160(%rsp), %rdi
callq 0x125a0
movsd 0x60(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
cmpl $0x1, 0x1e8(%rsp)
jne 0xc5f1
movq 0x1c8(%rsp), %rsi
movl 0x1c4(%rsp), %edx
movl 0x1c0(%rsp), %ecx
movl 0x1bc(%rsp), %r8d
movq %rsp, %rax
movl $0x0, (%rax)
leaq 0xf0(%rsp), %rdi
leaq 0x160(%rsp), %r9
callq 0xad30
jmp 0xc5ef
jmp 0xc634
movq 0x1c8(%rsp), %rsi
movl 0x1c4(%rsp), %edx
movl 0x1c0(%rsp), %ecx
movl 0x1bc(%rsp), %r8d
movl 0x1e8(%rsp), %edi
movq %rsp, %rax
movl %edi, (%rax)
leaq 0xf0(%rsp), %rdi
leaq 0x160(%rsp), %r9
callq 0xb850
jmp 0xc632
jmp 0xc634
movsd 0x140(%rsp), %xmm1
cvtsi2sdl 0x110(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0xc687
callq 0x4f20
movsd %xmm0, 0x50(%rsp)
cvtsi2sdl 0x110(%rsp), %xmm0
subsd 0x140(%rsp), %xmm0
divsd 0x138(%rsp), %xmm0
callq 0x4650
movsd 0x50(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xc831
cvtsi2sdl 0x110(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movl 0x19c(%rsp), %eax
cmpl 0x110(%rsp), %eax
jge 0xc82f
movl 0x110(%rsp), %eax
movl %eax, 0x19c(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4040
jmp 0xc6d2
movq 0x3d90f(%rip), %rdi     # 0x49fe8
leaq 0x29f53(%rip), %rsi     # 0x36633
callq 0x43c0
movq %rax, 0x48(%rsp)
jmp 0xc6ec
movq 0x48(%rsp), %rdi
movq 0x1c8(%rsp), %rax
movl (%rax), %esi
callq 0x4600
movq %rax, 0x40(%rsp)
jmp 0xc707
movq 0x40(%rsp), %rdi
leaq 0x29eb9(%rip), %rsi     # 0x365cc
callq 0x43c0
movq %rax, 0x38(%rsp)
jmp 0xc71f
movq 0x38(%rsp), %rdi
movl 0x1c4(%rsp), %esi
callq 0x4600
movq %rax, 0x30(%rsp)
jmp 0xc737
movq 0x30(%rsp), %rdi
leaq 0x29ef8(%rip), %rsi     # 0x3663b
callq 0x43c0
movq %rax, 0x28(%rsp)
jmp 0xc74f
movq 0x28(%rsp), %rdi
movl 0x134(%rsp), %esi
callq 0x4600
movq %rax, 0x20(%rsp)
jmp 0xc767
movq 0x20(%rsp), %rdi
leaq 0x29ed4(%rip), %rsi     # 0x36647
callq 0x43c0
movq %rax, 0x18(%rsp)
jmp 0xc77f
movq 0x18(%rsp), %rdi
movl 0x19c(%rsp), %esi
callq 0x4600
movq %rax, 0x10(%rsp)
jmp 0xc797
movq 0x10(%rsp), %rdi
leaq 0x29e79(%rip), %rsi     # 0x3661c
callq 0x43c0
movq %rax, 0x8(%rsp)
jmp 0xc7af
movq 0x8(%rsp), %rdi
movq 0x3d7d5(%rip), %rsi     # 0x49f90
callq 0x4400
jmp 0xc7c2
movq 0x1c8(%rsp), %rax
movl (%rax), %edi
movl 0x1c4(%rsp), %esi
movl 0x19c(%rsp), %edx
movq 0x1b0(%rsp), %r8
leaq 0x178(%rsp), %rcx
callq 0x9d60
jmp 0xc7f1
jmp 0xc82f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xc895
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x14600
jmp 0xc895
jmp 0xc84a
leaq 0x160(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x138e0
jmp 0xc848
jmp 0xc84a
leaq 0xf0(%rsp), %rdi
callq 0x14600
leaq 0x118(%rsp), %rdi
callq 0x12400
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
movsd 0x1a0(%rsp), %xmm0
mulsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
jmp 0xc2f5
leaq 0x118(%rsp), %rdi
callq 0x12400
jmp 0xc8ed
movq 0xd8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
leaq 0x19c(%rsp), %rdx
callq 0x14610
jmp 0xc8c3
leaq 0x160(%rsp), %rdi
callq 0x12400
leaq 0x178(%rsp), %rdi
callq 0x4700
movq 0xe0(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
retq
leaq 0x160(%rsp), %rdi
callq 0x12400
leaq 0x178(%rsp), %rdi
callq 0x4700
movq 0x150(%rsp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)