// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x29e0
movq -0x10(%rbp), %rdi
callq 0x2a00
incq %rax
movq %rax, -0xb0(%rbp)
movl -0x4(%rbp), %eax
incl %eax
movq %rax, -0xa8(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
leaq -0x55(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x2a20
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rcx
leaq -0x50(%rbp), %rdi
leaq -0x54(%rbp), %rdx
callq 0x2a40
jmp 0x2516
leaq -0x65(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x2ac0
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rcx
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x2ae0
jmp 0x2543
leaq -0x65(%rbp), %rdi
callq 0x2b60
leaq -0x50(%rbp), %rdi
callq 0x2b80
leaq -0x55(%rbp), %rdi
callq 0x2be0
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2a00
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq %rcx, %rax
ja 0x25f0
movq -0x70(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2c20
movl $0x0, (%rax)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x2566
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x25e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x65(%rbp), %rdi
callq 0x2b60
leaq -0x50(%rbp), %rdi
callq 0x2b80
leaq -0x55(%rbp), %rdi
callq 0x2be0
jmp 0x29c8
leaq -0x78(%rbp), %rdi
callq 0x2c40
movq $0x1, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2a00
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
ja 0x2860
movq $0x1, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl -0x4(%rbp), %ecx
cmpq %rcx, %rax
ja 0x284d
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
callq 0x2c60
movl (%rax), %eax
cmpq -0x88(%rbp), %rax
jbe 0x26f3
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
cmpl $-0x1, (%rax)
jne 0x2698
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xcc(%rbp)
jmp 0x26c3
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0x80(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
movl -0xd0(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
callq 0x2c60
movl (%rax), %eax
cmpq -0x88(%rbp), %rax
ja 0x2834
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
cmpl $-0x1, (%rax)
je 0x2789
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
movq -0x80(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
movl -0xd4(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x2801
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, -0xe0(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rsi
subq $0x1, %rsi
callq 0x2c60
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movl (%rax), %eax
subq %rax, %rsi
callq 0x2c20
cmpl $-0x1, (%rax)
je 0x27ff
movq -0x80(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
movl $0x0, (%rax)
jmp 0x2801
movq -0x80(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movq -0x88(%rbp), %rsi
callq 0x2c20
cmpl $-0x1, (%rax)
je 0x2832
leaq -0x78(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x2c80
jmp 0x2834
jmp 0x2836
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x2633
jmp 0x284f
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x2601
leaq -0x78(%rbp), %rdi
callq 0x2ce0
testb $0x1, %al
jne 0x286f
jmp 0x288d
leaq -0x78(%rbp), %rdi
callq 0x2d00
movq %rax, -0xf0(%rbp)
jmp 0x2881
movq -0xf0(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x28c0
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, -0x8c(%rbp)
jmp 0x29b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2db0
jmp 0x29c8
leaq -0x78(%rbp), %rdi
callq 0x2d00
movq %rax, -0xf8(%rbp)
jmp 0x28d2
movq -0xf8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
leaq -0x78(%rbp), %rdi
callq 0x2d00
movq %rax, -0x100(%rbp)
jmp 0x28f3
movq -0x100(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
je 0x29ac
movq -0x10(%rbp), %rdi
callq 0x2a00
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x2c00
movq %rax, %rdi
movslq -0x90(%rbp), %rsi
callq 0x2c20
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2a00
movl -0x94(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
decl %eax
movslq %eax, %rsi
callq 0x2c60
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x2d40
jmp 0x2982
movq -0x10(%rbp), %rdi
movl -0x98(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x2c60
movl (%rax), %ecx
movl -0x90(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x90(%rbp)
jmp 0x2902
movl $0x0, -0x8c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2db0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2190
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)