#import <stdio.h>

int main() {
    long N,x;
    scanf("%ld %ld", &N,&x);
    long cost = 0;
    long last;
    for (long i=0; i<N; i++){
        long a;
        scanf("%ld",&a);
        if (i==0) {
            if (a>x) {
                last = x;
                cost = a - x;
            } else {
                last = a;
            }
            continue;
        }
        if (last + a <= x) {
            last = a;
            continue;
        }
        last = x - last;
        cost += a - last;
    }
    
    printf("%ld", cost);
}

; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_876/source.m'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_876/source.m"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [8 x i8] c"%ld %ld\00", align 1
@.str.1 = private unnamed_addr constant [4 x i8] c"%ld\00", align 1

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
entry:
  %N = alloca i64, align 8
  %x = alloca i64, align 8
  %a = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %N) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %x) #4
  %call = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %N, ptr noundef nonnull %x)
  %0 = load i64, ptr %N, align 8, !tbaa !5
  %cmp21 = icmp sgt i64 %0, 0
  br i1 %cmp21, label %cleanup.peel, label %for.cond.cleanup

cleanup.peel:                                     ; preds = %entry
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %a) #4
  %call1.peel = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %a)
  %1 = load i64, ptr %a, align 8, !tbaa !5
  %2 = load i64, ptr %x, align 8, !tbaa !5
  %cmp3.peel = icmp sgt i64 %1, %2
  %sub.peel = sub nsw i64 %1, %2
  %cost.1.peel = select i1 %cmp3.peel, i64 %sub.peel, i64 0
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %a) #4
  %3 = load i64, ptr %N, align 8, !tbaa !5
  %cmp.peel = icmp sgt i64 %3, 1
  br i1 %cmp.peel, label %if.end5.preheader, label %for.cond.cleanup

if.end5.preheader:                                ; preds = %cleanup.peel
  %last.1.peel = call i64 @llvm.smin.i64(i64 %1, i64 %2)
  br label %if.end5

for.cond.cleanup:                                 ; preds = %if.end5, %cleanup.peel, %entry
  %cost.0.lcssa = phi i64 [ 0, %entry ], [ %cost.1.peel, %cleanup.peel ], [ %cost.2, %if.end5 ]
  %call13 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %cost.0.lcssa)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %x) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %N) #4
  ret i32 0

if.end5:                                          ; preds = %if.end5.preheader, %if.end5
  %i.024 = phi i64 [ %inc, %if.end5 ], [ 1, %if.end5.preheader ]
  %last.023 = phi i64 [ %last.2, %if.end5 ], [ %last.1.peel, %if.end5.preheader ]
  %cost.022 = phi i64 [ %cost.2, %if.end5 ], [ %cost.1.peel, %if.end5.preheader ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %a) #4
  %call1 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %a)
  %4 = load i64, ptr %a, align 8, !tbaa !5
  %add = add nsw i64 %4, %last.023
  %5 = load i64, ptr %x, align 8, !tbaa !5
  %cmp6.not = icmp sgt i64 %add, %5
  %sub9 = sub nsw i64 %5, %last.023
  %sub10 = add i64 %4, %cost.022
  %add11 = sub i64 %sub10, %sub9
  %cost.2 = select i1 %cmp6.not, i64 %add11, i64 %cost.022
  %last.2 = select i1 %cmp6.not, i64 %sub9, i64 %4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %a) #4
  %inc = add nuw nsw i64 %i.024, 1
  %6 = load i64, ptr %N, align 8, !tbaa !5
  %cmp = icmp slt i64 %inc, %6
  br i1 %cmp, label %if.end5, label %for.cond.cleanup, !llvm.loop !9
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #3

attributes #0 = { nofree nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.0.0 (++20231007042255+8abb2ace888b-1~exp1~20231007042414.1230)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.peeled.count", i32 1}
