unittest
{
	assert( [ "3 3", "2 2 2" ].parse.expand.solve == 1 );
	assert( [ "6 1", "1 6 1 2 0 4" ].parse.expand.solve == 11 );
	assert( [ "5 9", "3 1 4 1 5" ].parse.expand.solve == 0 );
	assert( [ "2 0", "5 5" ].parse.expand.solve == 10 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nx = input.front.split.to!( long[] );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( nx[ 1 ], as );
}

auto solve( long x, long[] as )
{
	auto ans = 0L;
	for( auto i = 0L; i < as.length - 1; i++ )
	{
		auto d = as[ i ] + as[ i + 1 ] - x;
		if( 0 < d )
		{
			// 2つの箱の総和がxを超えていた場合、右の箱を優先して食べる
			ans += d;
			if( d <= as[ i + 1 ] )
			{
				as[ i + 1 ] -= d;
			}
			else
			{
				as[ i ] -= d - as[ i + 1 ];
				as[ i + 1 ] = 0;
			}
		}
	}
	return ans;
}
