// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, 0x8(%rsp)
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x13fb
movq %rax, 0x8(%rbx)
movq (%r14), %rbp
movq 0x8(%r14), %r13
movq 0x8(%rsp), %rax
leal 0x1(%rax), %esi
leaq 0x54(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x1638
subq %rbp, %r13
sarq $0x2, %r13
incq %r13
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x6(%rsp), %rcx
movq %r13, %rsi
callq 0x16b6
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x145c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x10e0
movq (%r14), %rax
movq 0x8(%r14), %rdx
movq %rdx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
movq 0x38(%rsp), %rdi
leaq 0x1(%rsi), %rcx
cmpq $0x1, %rcx
adcq $0x1, %rsi
movq (%rdi), %r8
movl $0x0, (%r8)
addq $0x18, %rdi
decq %rsi
jne 0x147e
cmpq %rax, %rdx
je 0x1574
movl 0x8(%rsp), %edx
cmpq $0x3, %rcx
movl $0x2, %esi
cmovaeq %rcx, %rsi
movq %rsi, 0x18(%rsp)
negq %rdx
movl $0x1, %edi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0x1, %r9d
cmpl $0x0, 0x8(%rsp)
je 0x1564
movq 0x38(%rsp), %rsi
leaq (%rdi,%rdi,2), %r11
leaq (%rsi,%r11,8), %r10
addq $-0x18, %r10
leaq (%rsi,%r11,8), %r11
movl $0x1, %r13d
movl -0x4(%rax,%rdi,4), %esi
cmpq %rsi, %r13
jae 0x1509
movq (%r10), %rsi
movl (%rsi,%r13,4), %esi
incl %esi
cmovel %r8d, %esi
movq (%r11), %r15
movl %esi, (%r15,%r13,4)
movl -0x4(%rax,%rdi,4), %r12d
cmpq %r12, %r13
jb 0x1554
movq (%r10), %rsi
movl (%rsi,%r13,4), %r15d
cmpl $-0x1, %r15d
je 0x1525
incl %r15d
jmp 0x1536
shlq $0x2, %r12
subq %r12, %rsi
xorl %r15d, %r15d
cmpl $-0x1, (%rsi,%r13,4)
je 0x153d
movq (%r11), %rsi
movl %r15d, (%rsi,%r13,4)
movq (%r11), %rsi
cmpl $-0x1, (%rsi,%r13,4)
je 0x1554
movzbl %cl, %ecx
testb $0x1, %cl
cmovel %r9d, %ecx
movl %r13d, %ebp
leaq (%rdx,%r13), %rsi
incq %rsi
incq %r13
cmpq $0x1, %rsi
jne 0x14ec
incq %rdi
cmpq 0x18(%rsp), %rdi
jne 0x14c6
jmp 0x1576
xorl %ecx, %ecx
testb $0x1, %cl
je 0x15dd
testl %ebp, %ebp
je 0x15dd
movq 0x10(%rsp), %rax
movl %ebp, (%rax)
movq (%r14), %rax
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 0x15ca
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x15d2
movq %rbx, %rdi
callq 0x190e
movq (%r14), %rax
subl (%rax,%r15,4), %ebp
jne 0x1586
jmp 0x15e8
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
leaq 0x38(%rsp), %rdi
callq 0x174e
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 0x162f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x10e0
jmp 0x162f
movq %rax, %rbx
jmp 0x162f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x174e
movq %rbx, %rdi
callq 0x1160
nop