using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Text; using static System.Math; using static System.Linq.Enumerable; using static Extentions; public static class Program { public static void Solve() { const int wMax = 1000000; var (n, k) = (I, I); var segtree = new SegmentTree(wMax + 1, Monoid.Sum_Int32); Repeat(n, () => { var w = I; if (w > 0 && segtree[w, wMax] < k) segtree[w]++; if (w < 0 && segtree[-w] > 0) segtree[-w]--; }); Console.WriteLine(segtree[0, wMax]); } #region Scanners static Scanner _scanner; static char C => _scanner.NextChar(); static string S => _scanner.NextString(); static int I => _scanner.NextInt(); static long L => _scanner.NextLong(); static BigInteger B => _scanner.NextBigInteger(); static double D => _scanner.NextDouble(); static decimal M => _scanner.NextDecimal(); #endregion public static void Main() { var sw = new StreamWriter(Console.OpenStandardOutput()); sw.NewLine = "\n"; #if DEBUG sw.AutoFlush = true; Console.SetError(sw); #else sw.AutoFlush = false; #endif Console.SetOut(sw); _scanner = new Scanner(Console.OpenStandardInput()); Solve(); Console.Out.Flush(); } } public static partial class Extentions { } #region Library // not tested public class Scanner { private readonly Stream _stream; private const int _bufferSize = 1024; private readonly byte[] _buf = new byte[_bufferSize]; private int _len, _ptr; public Scanner(Stream stream) { _stream = stream; } public byte ReadByte() { if (_ptr >= _len) { _len = _stream.Read(_buf, 0, 1024); _ptr = 0; } return _buf[_ptr++]; } public char ReadChar() => (char)ReadByte(); public string ReadLine() { var r = new StringBuilder(); if (_ptr == 0) r.Append(ReadChar()); for (; _ptr < _len; _ptr++) r.Append((char)_buf[_ptr]); return r.ToString(); } public char NextChar() => char.Parse(NextString()); public string NextString() { var r = new StringBuilder(); var b = ReadChar(); while (b != ' ' && b != '\n') { r.Append(b); b = ReadChar(); } return r.ToString(); } public int NextInt() => (int)NextLong(); public long NextLong() { var r = 0L; var b = ReadByte(); var n = b == '-'; if (n) b = ReadByte(); while (b != ' ' && b != '\n') { r = r * 10 + b - '0'; b = ReadByte(); } return n ? -r : r; } public BigInteger NextBigInteger() { var r = new BigInteger(); var b = ReadByte(); var n = b == '-'; if (n) b = ReadByte(); while (b != ' ' && b != '\n') { r = r * 10 + b - '0'; b = ReadByte(); } return n ? -r : r; } public double NextDouble() { var i = 0L; var b = ReadByte(); var n = b == '-'; if (n) b = ReadByte(); while (b != '.') { i = i * 10 + b - '0'; b = ReadByte(); } b = ReadByte(); var f = 0L; var p = 0; while (b != ' ' && b != '\n') { f = f * 10 + b - '0'; b = ReadByte(); p++; } var r = i + (double)f / Pow(10, p); return n ? -r : r; } public decimal NextDecimal() => decimal.Parse(NextString()); public T Next(Func parser) => parser(NextString()); } public class Monoid { public Monoid(T unit, Func append) { this.Unit = unit; this.Append = append; } public T Unit { get; } public Func Append { get; } } public static class Monoid { public static Monoid Create(T unit, Func append) => new Monoid(unit, append); public static Monoid Sum_Int32 => new Monoid(0, (x, y) => x + y); public static Monoid Min_Int32 => new Monoid(int.MaxValue, Min); public static Monoid Max_Int32 => new Monoid(int.MinValue, Max); } public class SegmentTree : IEnumerable, IReadOnlyList { private T[] _tree; private int _size; private Monoid _monoid; public SegmentTree(int length, Monoid monoid) { if (length > (int.MaxValue >> 1) + 1) throw new ArgumentException(); _size = 1; while (_size < length) _size <<= 1; _tree = Repeat(monoid.Unit, _size << 1).ToArray(); _monoid = monoid; this.Length = length; } public SegmentTree(IList collection, Monoid monoid) { if (collection.Count > (int.MaxValue >> 1) + 1) throw new ArgumentException(); _size = 1; while (_size < collection.Count) _size <<= 1; _tree = new T[_size << 1]; for (var i = 0; i < _size; i++) _tree[i + _size] = i < collection.Count ? collection[i] : monoid.Unit; for (var i = _size - 1; i > 0; i--) _tree[i] = monoid.Append(_tree[i << 1], _tree[(i << 1) + 1]); _monoid = monoid; this.Length = collection.Count; } public int Length { get; } int IReadOnlyCollection.Count => this.Length; public T this[int i] { get => _tree[i + _size]; set { _tree[i += _size] = value; for (i >>= 1; i > 0; i >>= 1) _tree[i] = _monoid.Append(_tree[i << 1], _tree[(i << 1) + 1]); } } public T this[int i, int j] { get { var r = _monoid.Unit; i += _size; j += _size + 1; for (; i < j; i >>= 1, j >>= 1) { if ((j & 1) != 0) r = _monoid.Append(r, _tree[--j]); if ((i & 1) != 0) r = _monoid.Append(r, _tree[i++]); } return r; } } public IEnumerator GetEnumerator() { for (var i = 0; i < this.Length; i++) yield return this[i]; } IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); } public static partial class Extentions { [DebuggerStepThrough] public static void Assert(bool condition) { if (!condition) throw new Exception("Assertion failed"); } public static string AsString(this IEnumerable source) => new string(source.ToArray()); public static void ForEach(this IEnumerable source, Action action) { foreach (var item in source) action(item); } public static void ForEach(this IEnumerable source, Func func) { foreach (var item in source) func(item); } public static void ForEach(this IEnumerable source, Action action) { var i = 0; foreach (var item in source) action(item, i++); } public static void ForEach(this IEnumerable source, Func func) { var i = 0; foreach (var item in source) func(item, i++); } public static void Repeat(int count, Action action) { for (var i = 0; i < count; i++) action(); } public static void Repeat(int count, Action action) { for (var i = 0; i < count; i++) action(i); } public static IEnumerable Repeat(Func func) { for (var i = 0; ; i++) yield return func(); } public static IEnumerable Repeat(int count, Func func) { for (var i = 0; i < count; i++) yield return func(); } public static IEnumerable Repeat(Func func) { for (var i = 0; ; i++) yield return func(i); } public static IEnumerable Repeat(int count, Func func) { for (var i = 0; i < count; i++) yield return func(i); } public static void Swap(ref T x, ref T y) { var tmp = x; x = y; y = tmp; } public static (T1[], T2[]) Unzip(this ICollection<(T1, T2)> source) { var ts1 = new T1[source.Count]; var ts2 = new T2[source.Count]; var i = 0; foreach (var (t1, t2) in source) { ts1[i] = t1; ts2[i] = t2; i++; } return (ts1, ts2); } public static (T1[], T2[], T3[]) Unzip(this ICollection<(T1, T2, T3)> source) { var ts1 = new T1[source.Count]; var ts2 = new T2[source.Count]; var ts3 = new T3[source.Count]; var i = 0; foreach (var (t1, t2, t3) in source) { ts1[i] = t1; ts2[i] = t2; ts3[i] = t3; i++; } return (ts1, ts2, ts3); } public static (T1[], T2[], T3[], T4[]) Unzip(this ICollection<(T1, T2, T3, T4)> source) { var ts1 = new T1[source.Count]; var ts2 = new T2[source.Count]; var ts3 = new T3[source.Count]; var ts4 = new T4[source.Count]; var i = 0; foreach (var (t1, t2, t3, t4) in source) { ts1[i] = t1; ts2[i] = t2; ts3[i] = t3; ts4[i] = t4; i++; } return (ts1, ts2, ts3, ts4); } public static IEnumerable Zip(this IEnumerable first, IEnumerable second, IEnumerable thrid, Func resultSelector) { using (var e1 = first.GetEnumerator()) using (var e2 = second.GetEnumerator()) using (var e3 = thrid.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext()) yield return resultSelector(e1.Current, e2.Current, e3.Current); } } public static IEnumerable Zip(this IEnumerable first, IEnumerable second, IEnumerable thrid, IEnumerable fourth, Func resultSelector) { using (var e1 = first.GetEnumerator()) using (var e2 = second.GetEnumerator()) using (var e3 = thrid.GetEnumerator()) using (var e4 = fourth.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext() && e4.MoveNext()) yield return resultSelector(e1.Current, e2.Current, e3.Current, e4.Current); } } } #endregion