#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ optimal_weight(int, int *, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int W;
  int num_bar, i;
  scanf("%d %d", &W, &num_bar);
  int *bars = (int *)malloc(sizeof(int) * num_bar);
  i = 0;
  while (i < num_bar) {
    scanf("%d", bars + i);
    i++;
  }
  printf("%ld\n", optimal_weight(W, bars, num_bar));
  free(bars);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1070 <scanf@plt>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c6 <main+0x76>
mov    -0x18(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118f <main+0x3f>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <optimal_weight@plt>
mov    %eax,%esi
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    (%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    (%rsp),%edx
test   %edx,%edx
jle    11ae <main+0x5e>
lea    0xe79(%rip),%r15
mov    %r14,%r12
xor    %ebx,%ebx
nop
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
add    $0x1,%rbx
movslq (%rsp),%rdx
add    $0x4,%r12
cmp    %rdx,%rbx
jl     1190 <main+0x40>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
call   1040 <optimal_weight@plt>
lea    0xe45(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = mul i64 4, %8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %5
store i32 0, i32* %4
br label %13
13: ; preds = %17, %0
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = load i32*, i32** %5
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
%22 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %13
25: ; preds = %13
%26 = load i32, i32* %2
%27 = load i32*, i32** %5
%28 = load i32, i32* %3
%29 = call i32 @optimal_weight(i32 %26, i32* %27, i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32*, i32** %5
%32 = call i32 @free(i32* %31)
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%6 = load i32, i32* %2
%7 = shl i32 %6, 2
%8 = call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i32*
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %0, %12
%13 = phi i64 [ %16, %12 ], [ 0, %0 ]
%14 = getelementptr inbounds i32, i32* %9, i64 %13
%15 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %14)
%16 = add nuw nsw i64 %13, 1
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = icmp slt i64 %16, %18
br i1 %19, label %12, label %20
20: ; preds = %12, %0
%21 = phi i32 [ %10, %0 ], [ %17, %12 ]
%22 = load i32, i32* %1
%23 = call i32 @optimal_weight(i32 %22, i32* %9, i32 %21)
%24 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @free(i32* %9)
ret i32 0
}