Submission #46314486


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks;

public class D
{
    public static void Main()
    {
        using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }

    public static void Solve()
    {
        var N = Scanner.Scan<int>();
        var dict = new Dictionary<long, long>();
        var queue = new PriorityQueue<long>();
        for (var i = 0; i < N; i++)
        {
            var (s, c) = Scanner.Scan<long, long>();
            if (!dict.ContainsKey(s)) dict[s] = 0;
            dict[s] += c;
            queue.Enqueue(s);
        }

        while (queue.Count > 0)
        {
            var s = queue.Dequeue();
            var c = dict[s];
            var ns = s * 2;
            var nc = c / 2;
            if (nc > 0)
            {
                if (!dict.ContainsKey(ns)) dict[ns] = 0;
                dict[ns] += nc;
                dict[s] -= nc * 2;
                queue.Enqueue(ns);
            }
        }

        var answer = dict.Values.Sum();
        Console.WriteLine(answer);
    }

    public class PriorityQueue<T> : IReadOnlyCollection<T>
    {
        private readonly Comparison<T> _comparison;
        private readonly List<T> _heap;
        public PriorityQueue(IEnumerable<T> items, IComparer<T> comparer = null) : this(comparer)
        {
            foreach (var item in items) Enqueue(item);
        }
        public PriorityQueue(IEnumerable<T> items, Comparison<T> comparison) : this(comparison)
        {
            foreach (var item in items) Enqueue(item);
        }
        public PriorityQueue(IComparer<T> comparer = null) : this((comparer ?? Comparer<T>.Default).Compare) { }
        public PriorityQueue(Comparison<T> comparison)
        {
            _heap = new List<T>();
            _comparison = comparison;
        }
        public int Count => _heap.Count;
        public IEnumerator<T> GetEnumerator() => _heap.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
        public void Enqueue(T item)
        {
            var child = Count;
            _heap.Add(item);
            while (child > 0)
            {
                var parent = (child - 1) / 2;
                if (_comparison(_heap[parent], _heap[child]) <= 0) break;
                (_heap[parent], _heap[child]) = (_heap[child], _heap[parent]);
                child = parent;
            }
        }
        public T Dequeue()
        {
            if (Count == 0) throw new InvalidOperationException();
            var result = _heap[0];
            _heap[0] = _heap[Count - 1];
            _heap.RemoveAt(Count - 1);
            var parent = 0;
            while (parent * 2 + 1 < Count)
            {
                var left = parent * 2 + 1;
                var right = parent * 2 + 2;
                if (right < Count && _comparison(_heap[left], _heap[right]) > 0)
                    left = right;
                if (_comparison(_heap[parent], _heap[left]) <= 0) break;
                (_heap[parent], _heap[left]) = (_heap[left], _heap[parent]);
                parent = left;
            }
            return result;
        }
        public T Peek()
        {
            if (Count == 0) throw new InvalidOperationException();
            return _heap[0];
        }
        public bool TryDequeue(out T result)
        {
            if (Count > 0)
            {
                result = Dequeue();
                return true;
            }
            result = default;
            return false;
        }
        public bool TryPeek(out T result)
        {
            if (Count > 0)
            {
                result = Peek();
                return true;
            }
            result = default;
            return false;
        }
        public void Clear() => _heap.Clear();
        public bool Contains(T item) => _heap.Contains(item);
    }

    public static class Scanner
    {
        public static T Scan<T>() where T : IConvertible => Convert<T>(ScanStringArray()[0]);
        public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]));
        }
        public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]));
        }
        public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]));
        }
        public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]));
        }
        public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]), Convert<T6>(input[5]));
        }
        public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => ScanStringArray().Select(Convert<T>);
        private static string[] ScanStringArray()
        {
            var line = Console.ReadLine()?.Trim() ?? string.Empty;
            return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
        }
        private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
    }
}

Submission Info

Submission Time
Task D - Merge Slimes
User AconCavy
Language C# 11.0 (.NET 7.0.7)
Score 425
Code Size 6501 Byte
Status AC
Exec Time 2626 ms
Memory 274936 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 33
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt
Case Name Status Exec Time Memory
example_00.txt AC 61 ms 26456 KiB
example_01.txt AC 58 ms 26560 KiB
example_02.txt AC 46 ms 26396 KiB
hand_00.txt AC 379 ms 58580 KiB
hand_01.txt AC 1561 ms 149668 KiB
hand_02.txt AC 2481 ms 274208 KiB
hand_03.txt AC 2512 ms 274936 KiB
hand_04.txt AC 378 ms 58352 KiB
hand_05.txt AC 57 ms 26692 KiB
hand_06.txt AC 57 ms 26484 KiB
hand_07.txt AC 1532 ms 148524 KiB
hand_08.txt AC 58 ms 26696 KiB
hand_09.txt AC 62 ms 26416 KiB
hand_10.txt AC 371 ms 57792 KiB
hand_11.txt AC 1510 ms 148196 KiB
hand_12.txt AC 2455 ms 271076 KiB
hand_13.txt AC 2472 ms 272100 KiB
hand_14.txt AC 376 ms 57832 KiB
random_00.txt AC 596 ms 71004 KiB
random_01.txt AC 729 ms 74600 KiB
random_02.txt AC 849 ms 89396 KiB
random_03.txt AC 919 ms 89372 KiB
random_04.txt AC 970 ms 90040 KiB
random_05.txt AC 1027 ms 91348 KiB
random_06.txt AC 624 ms 66616 KiB
random_07.txt AC 561 ms 64780 KiB
random_08.txt AC 2576 ms 180260 KiB
random_09.txt AC 2582 ms 180500 KiB
random_10.txt AC 2591 ms 180360 KiB
random_11.txt AC 2626 ms 180340 KiB
random_12.txt AC 2622 ms 180308 KiB
random_13.txt AC 2591 ms 180544 KiB
random_14.txt AC 2570 ms 180316 KiB