Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Problem3.GreedyTimes
- {
- class Program
- {
- static void Main(string[] args)
- {
- long bagCapacity = long.Parse(Console.ReadLine());
- var items = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- long goldSum = 0;
- var gems = new Dictionary<string, long>();
- long gemSum = 0;
- var cash = new Dictionary<string, long>();
- long cashSum = 0;
- for (int searchItem = 0; searchItem < items.Length; searchItem += 2)
- {
- if (items[searchItem] == "Gold")
- {
- long goldAmount = long.Parse(items[searchItem + 1]);
- if (bagCapacity >= goldAmount)
- {
- goldSum += goldAmount;
- bagCapacity -= goldAmount;
- }
- }
- else if (isGem(items[searchItem]))
- {
- long gemAmount = long.Parse(items[searchItem + 1]);
- if (bagCapacity >= gemAmount && gemAmount + gemSum <= goldSum)
- {
- gemSum += gemAmount;
- bagCapacity -= gemAmount;
- if (!gems.ContainsKey(items[searchItem]))
- {
- gems.Add(items[searchItem], gemAmount);
- }
- else
- {
- gems[items[searchItem]] += gemAmount;
- }
- }
- }
- else if (items[searchItem].Length == 3)
- {
- long cashAmount = long.Parse(items[searchItem + 1]);
- if (bagCapacity >= cashAmount && cashAmount + cashSum <= gemSum)
- {
- cashSum += cashAmount;
- bagCapacity -= cashAmount;
- if (!cash.ContainsKey(items[searchItem]))
- {
- cash.Add(items[searchItem], cashAmount);
- }
- else
- {
- cash[items[searchItem]] += cashAmount;
- }
- }
- }
- if (bagCapacity == 0)
- {
- break;
- }
- }
- if (goldSum != 0)
- {
- Console.WriteLine($"<Gold> ${goldSum}");
- Console.WriteLine($"##Gold - {goldSum}");
- }
- if (gemSum != 0)
- {
- Console.WriteLine($"<Gem> ${gemSum}");
- foreach (var gem in gems.OrderByDescending(x => x.Key).ThenBy(x => x.Value).Where(x => x.Value > 0))
- {
- Console.WriteLine($"##{gem.Key} - {gem.Value}");
- }
- }
- if (cashSum != 0)
- {
- Console.WriteLine($"<Cash> ${cashSum}");
- foreach (var item in cash.OrderByDescending(x => x.Key).ThenBy(x => x.Value).Where(x => x.Value > 0))
- {
- Console.WriteLine($"##{item.Key} - {item.Value}");
- }
- }
- }
- private static bool isGem(string gem)
- {
- if (gem.Length >= 4)
- {
- var exist = gem.Substring(gem.Length - 3, 3).ToLower();
- if (exist == "gem")
- {
- return true;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment