// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movsd %xmm1, 0x80(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %r9, 0x78(%rsp)
movl %edx, 0x14(%rsp)
movq %rsi, %r15
movq %rdi, %r12
leaq 0x50(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
movl $0x50, %edi
callq 0x3310
movq %rax, 0x28(%rsp)
movq %rax, %rcx
addq $0x50, %rcx
movq %rcx, 0x38(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rax)
movupd %xmm0, 0x30(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
movq %rcx, 0x30(%rsp)
xorl %ebx, %ebx
callq 0x3a53
movq %rax, %xmm0
punpckldq 0xcdb5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xcdbd(%rip), %xmm0     # 0x15020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xce29(%rip), %xmm1     # 0x150a0
addsd 0xce59(%rip), %xmm1     # 0x150d8
movq 0x28(%rsp), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
movq 0x30(%rsp), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rax
cmpq %rax, %rbx
jl 0x8249
movq %r12, 0x70(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x100(%rsp)
jle 0x8701
movsd 0xce10(%rip), %xmm0     # 0x150d0
movsd %xmm0, 0x18(%rsp)
xorl %r13d, %r13d
movq 0x14d18(%rip), %rbx     # 0x1cfe8
movl %r13d, %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x23, %rax
addl %eax, %eax
leal (%rax,%rax,4), %eax
cmpl %r13d, %eax
jne 0x839c
movl $0x8, %edx
movq %rbx, %rdi
leaq 0xd20e(%rip), %rsi      # 0x1550b
callq 0x3350
movq %rbx, %rdi
movl %r13d, %esi
callq 0x3490
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xd4b1(%rip), %rsi      # 0x157d0
callq 0x3350
movq %r14, %rdi
movl 0x100(%rsp), %esi
callq 0x3490
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0xd1cf(%rip), %rsi      # 0x15514
callq 0x3350
movq %r14, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x3440
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xcfc5(%rip), %rsi      # 0x1532f
callq 0x3350
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x3440
movq %rax, %r14
movl $0x9, %edx
movq %rax, %rdi
leaq 0xd189(%rip), %rsi      # 0x15518
callq 0x3350
movq %r14, %rdi
callq 0x3200
movsd 0xcd24(%rip), %xmm0     # 0x150c8
ucomisd 0x20(%rsp), %xmm0
jbe 0x840b
movl $0x29, %edx
movq %rbx, %rdi
leaq 0xd167(%rip), %rsi      # 0x15522
callq 0x3350
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movslq %ebp, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x22, %rax
addl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xc4a6
callq 0x3a53
movq %rax, %r12
callq 0x3a53
movq %rax, %r14
callq 0x3a53
shrq %rax
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
xorl %edx, %edx
divq %rsi
movq %r14, %xmm1
punpckldq 0xcbb8(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xcbc0(%rip), %xmm1     # 0x15020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0xcc2c(%rip), %xmm0     # 0x150a0
testb $0x2, %r12b
jne 0x8484
addsd 0xcc56(%rip), %xmm0     # 0x150d8
jmp 0x8499
mulsd 0xcc24(%rip), %xmm0     # 0x150b0
addsd 0xcc4c(%rip), %xmm0     # 0x150e0
addsd (%rcx,%rdx,8), %xmm0
movl 0x108(%rsp), %eax
movsd %xmm0, (%rcx,%rdx,8)
cmpl $0x1, %eax
jne 0x84cc
movl $0x0, (%rsp)
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
movl 0x14(%rsp), %edx
leaq 0x28(%rsp), %r9
callq 0x748f
jmp 0x84e8
movl %eax, (%rsp)
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
movl 0x14(%rsp), %edx
leaq 0x28(%rsp), %r9
callq 0x7bf2
movl 0xa8(%rsp), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd 0x18(%rsp), %xmm1
movsd %xmm0, 0x60(%rsp)
ucomisd %xmm1, %xmm0
jae 0x859a
movsd %xmm1, 0x18(%rsp)
callq 0x3a53
movq %rax, %xmm0
punpckldq 0xcaea(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xcaf2(%rip), %xmm0     # 0x15020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0xcb66(%rip), %xmm1     # 0x150a8
movapd %xmm1, 0x60(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xa8(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
callq 0x34c0
ucomisd 0x60(%rsp), %xmm0
jae 0x8584
leaq 0x28(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xc2d0
jmp 0x86af
movl 0xa8(%rsp), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x60(%rsp)
cmpl %r12d, %ebp
jge 0x86a3
leaq 0x40(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3180
movl $0x7, %edx
movq %rbx, %rdi
leaq 0xcf88(%rip), %rsi      # 0x1554c
callq 0x3350
movl (%r15), %esi
movq %rbx, %rdi
callq 0x3490
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xceff(%rip), %rsi      # 0x154e5
callq 0x3350
movq %r14, %rdi
movl 0x14(%rsp), %esi
callq 0x3490
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0xcf4b(%rip), %rsi      # 0x15554
callq 0x3350
movq %r14, %rdi
movl %r13d, %esi
callq 0x3490
movq %rax, %r14
movl $0x2, %edx
movq %rax, %rdi
leaq 0xcf35(%rip), %rsi      # 0x15560
callq 0x3350
movq %r14, %rdi
movl %r12d, %esi
callq 0x3490
movq %rax, %r14
movl $0x16, %edx
movq %rax, %rdi
leaq 0xcee8(%rip), %rsi      # 0x15535
callq 0x3350
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3300
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3200
movl (%r15), %edi
movl 0x14(%rsp), %esi
movl %r12d, %edx
leaq 0x40(%rsp), %rcx
movq 0x78(%rsp), %r8
callq 0x699c
movl %r12d, %ebp
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x86af
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x86c9
callq 0x32f0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x86db
callq 0x32f0
incl %r13d
movsd 0x20(%rsp), %xmm0
mulsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
cmpl %r13d, 0x100(%rsp)
jne 0x82d0
movq 0x70(%rsp), %r14
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0xeed8
movl %ebp, 0x20(%r14)
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
leaq 0x50(%rsp), %rbx
je 0x873a
callq 0x32f0
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8749
callq 0x32f0
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x87a2
movq %rax, %rbx
jmp 0x87b9
jmp 0x87a2
movq %rax, %rbx
jmp 0x8789
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x8789
callq 0x32f0
leaq 0x50(%rsp), %r14
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x87aa
callq 0x32f0
jmp 0x87aa
movq %rax, %rbx
leaq 0x50(%rsp), %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x87b9
callq 0x32f0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x87c8
callq 0x32f0
movq %rbx, %rdi
callq 0x34a0