// File: /workspace/github_cpp_makefile_star2_O3/A-deLuna[P]encora-cpp-test/largest_sum.cpp
// Function: find_largest_sum(unsigned int, std::vector<unsigned int, std::allocator<unsigned int>> const&, std::vector<unsigned int, std::allocator<unsigned int>>&, unsigned int&)
void find_largest_sum(const uint32_t T,
                      const std::vector<uint32_t>& I,
                      std::vector<uint32_t>& M,
                      uint32_t& S) {
  M.clear();
  // Traditional implementation uses booleans to indicate whether subset
  // adds up to j. We can optimize the path finding step by additionally storing
  // an offset to the first index i where I[0..i] sums up to j. The element in
  // this first index i is guaranteed to be part of a set that adds up to j.
  // Here -1 is the same as false and any other number is true.
  std::vector<std::vector<int>> dp(I.size() + 1, std::vector<int>(T + 1, -1));

  // Base case, we can always take an empty set to sum up to zero.
  for (size_t i = 0; i <= I.size(); i++) {
    dp[i][0] = 0;
  }

  std::optional<int32_t> maxSoFar;
  for (size_t i = 1; i <= I.size(); i++) {
    for (size_t j = 1; j <= T; j++) {
      if (I[i - 1] > j) {
        dp[i][j] = dp[i - 1][j] == -1 ? -1 : dp[i - 1][j] + 1;
      }
      if (I[i - 1] <= j) {
        if (dp[i - 1][j] != -1) {
          dp[i][j] = dp[i - 1][j] + 1;
        } else if (dp[i - 1][j - I[i - 1]] != -1) {
          dp[i][j] = 0;
        }
        if (dp[i][j] != -1) {
          // keep track of the greatest j <= T for which we have an answer.
          maxSoFar = j;
        }
      }
    }
  }
  // Could not find a subset in I with sum <= T.
  if (!maxSoFar.has_value() || maxSoFar.value() == 0) {
    S = 0;
    return;
  }

  S = maxSoFar.value();
  int current = maxSoFar.value();

  // All elements at position [I.size()][j] contain an offset to the
  // first index where the sum of elements in subset [0..i] equal to j.
  // This element is guaranteed to be part of the path.
  while (current != 0) {
    int offset = dp[I.size()][current];
    int idx = I.size() - offset;
    M.push_back(I[idx - 1]);
    current -= I[idx - 1];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movl %edi, %r12d
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x13f9
movq %rax, 0x8(%rbx)
movq (%r14), %rbp
movq 0x8(%r14), %r13
leal 0x1(%r12), %esi
leaq 0x54(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %rdi
leaq 0xf(%rsp), %rcx
callq 0x1642
subq %rbp, %r13
sarq $0x2, %r13
incq %r13
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %r13, %rsi
callq 0x17be
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1457
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x10e0
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
movq 0x38(%rsp), %rdi
leaq 0x1(%rdx), %rsi
cmpq $0x1, %rsi
adcq $0x1, %rdx
movq (%rdi), %r8
movl $0x0, (%r8)
addq $0x18, %rdi
decq %rdx
jne 0x1479
cmpq %rax, %rcx
je 0x15e7
movq %r14, 0x18(%rsp)
movl %r12d, %ecx
cmpq $0x3, %rsi
movl $0x2, %edx
cmovaeq %rsi, %rdx
negq %rcx
movl $0x1, %edi
xorl %esi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0x1, %r9d
testl %r12d, %r12d
je 0x156f
movq 0x38(%rsp), %r10
leaq (%rdi,%rdi,2), %r11
leaq (%r10,%r11,8), %r10
addq $-0x18, %r10
movl $0x1, %r11d
movl -0x4(%rax,%rdi,4), %r13d
cmpq %r13, %r11
jae 0x1505
movq (%r10), %r14
movl (%r14,%r11,4), %r14d
incl %r14d
cmovel %r8d, %r14d
movq 0x18(%r10), %r15
movl %r14d, (%r15,%r11,4)
movl -0x4(%rax,%rdi,4), %r13d
cmpq %r13, %r11
jb 0x1553
movq (%r10), %r14
movl (%r14,%r11,4), %r15d
cmpl $-0x1, %r15d
je 0x152a
incl %r15d
movq 0x18(%r10), %r14
movl %r15d, (%r14,%r11,4)
cmpl $-0x1, %r15d
jne 0x1544
jmp 0x1553
shlq $0x2, %r13
subq %r13, %r14
cmpl $-0x1, (%r14,%r11,4)
movq 0x18(%r10), %r14
je 0x1569
movl $0x0, (%r14,%r11,4)
movzbl %sil, %esi
testb $0x1, %sil
cmovel %r9d, %esi
movl %r11d, %ebp
leaq (%rcx,%r11), %r14
incq %r14
incq %r11
cmpq $0x1, %r14
jne 0x14e0
jmp 0x156f
movl (%r14,%r11,4), %r15d
jmp 0x1522
incq %rdi
cmpq %rdx, %rdi
jne 0x14c0
testb $0x1, %sil
movq 0x18(%rsp), %r14
je 0x15e7
testl %ebp, %ebp
je 0x15e7
movq 0x10(%rsp), %rcx
movl %ebp, (%rcx)
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x38(%rsp), %rdx
leaq (%rcx,%rcx,2), %rsi
movslq %ebp, %rdi
movq (%rdx,%rsi,8), %rdx
movl (%rdx,%rdi,4), %edx
notl %edx
addl %ecx, %edx
movslq %edx, %r15
leaq (%rax,%r15,4), %rdx
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x15d2
movl (%rdx), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x15e1
movq %rbx, %rdi
callq 0x1a1c
movq (%r14), %rax
movl (%rax,%r15,4), %ecx
subl %ecx, %ebp
jne 0x1591
jmp 0x15f2
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
leaq 0x38(%rsp), %rdi
callq 0x1856
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1639
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x10e0
jmp 0x1639
movq %rax, %rbx
jmp 0x1639
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1856
movq %rbx, %rdi
callq 0x1160
nop