#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 calc(int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int n, m, i, j, temp, sum, max, ele;
  scanf("%d %d", &n, &m);
  sum = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &ele);
    temp = calc(ele, m);
    if (sum + temp > m)
      sum = (sum % m + temp % m) % m;
    else
      sum += temp;
  }
  printf("%d\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11dc <main+0xac>
lea    0xe8d(%rip),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <calc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    11c0 <main+0x90>
mov    -0x1c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    -0x28(%rbp),%eax
add    %edx,%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x1c(%rbp)
jmp    11c9 <main+0x99>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <main+0x9e>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1163 <main+0x33>
mov    -0x1c(%rbp),%esi
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xebd(%rip),%rdi
xor    %r12d,%r12d
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11bd <main+0x8d>
lea    0xe9e(%rip),%r14
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    117c <main+0x4c>
add    $0x1,%ebp
mov    %r12d,%ebx
cmp    0x8(%rsp),%ebp
jge    11bd <main+0x8d>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0xc(%rsp),%edi
mov    0x4(%rsp),%esi
call   1040 <calc@plt>
mov    %eax,%ecx
lea    (%rcx,%rbx,1),%r12d
mov    0x4(%rsp),%edi
cmp    %edi,%r12d
jle    1170 <main+0x40>
mov    %ebx,%eax
cltd
idiv   %edi
mov    %edx,%esi
mov    %ecx,%eax
cltd
idiv   %edi
add    %edx,%esi
mov    %esi,%eax
cltd
idiv   %edi
mov    %edx,%r12d
jmp    1170 <main+0x40>
lea    0xe42(%rip),%rdi
mov    %r12d,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
store i32 0, i32* %7
store i32 0, i32* %4
br label %11
11: ; preds = %40, %0
%12 = load i32, i32* %4
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %43
15: ; preds = %11
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%17 = load i32, i32* %9
%18 = load i32, i32* %3
%19 = call i32 @calc(i32 %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = add nsw i32 %20, %21
%23 = load i32, i32* %3
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %15
%26 = load i32, i32* %7
%27 = load i32, i32* %3
%28 = srem i32 %26, %27
%29 = load i32, i32* %6
%30 = load i32, i32* %3
%31 = srem i32 %29, %30
%32 = add nsw i32 %28, %31
%33 = load i32, i32* %3
%34 = srem i32 %32, %33
store i32 %34, i32* %7
br label %39
35: ; preds = %15
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = add nsw i32 %37, %36
store i32 %38, i32* %7
br label %39
39: ; preds = %35, %25
br label %40
40: ; preds = %39
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %11
43: ; preds = %11
%44 = load i32, i32* %7
%45 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %44)
ret i32 0
}

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