miroslavbstoyanov

Untitled

Feb 8th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._Greedy_Times
  6. {
  7. class Program
  8. {
  9.  
  10. static void Main(string[] args)
  11. {
  12. long capacity = long.Parse(Console.ReadLine());
  13. string[] input = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  14.  
  15. var goldDict = new Dictionary<string, long>();
  16. var gemDict = new Dictionary<string, long>();
  17. var cashDict = new Dictionary<string, long>();
  18.  
  19. long goldAmound = 0;
  20. long gemAmound = 0;
  21. long cashdAmound = 0;
  22.  
  23.  
  24. for (int i = 0; i < input.Length; i += 2)
  25. {
  26. string item = input[i];
  27. long itemValue = long.Parse(input[i + 1]);
  28. bool capacityIsValid = goldAmound + gemAmound + cashdAmound + itemValue <= capacity;
  29.  
  30. if (!capacityIsValid)
  31. {
  32. continue;
  33. }
  34.  
  35. if (item.ToLower() == "gold")
  36. {
  37. NewItemInTheBag(goldDict, item, itemValue);
  38. goldAmound += itemValue;
  39. }
  40. else if (item.Length >= 4 && IsValid(itemValue, gemAmound, goldAmound))
  41. {
  42. NewItemInTheBag(gemDict, item, itemValue);
  43. gemAmound += itemValue;
  44. }
  45. else if (item.Length == 3 && IsValid(itemValue, cashdAmound, gemAmound))
  46. {
  47. NewItemInTheBag(cashDict, item, itemValue);
  48. cashdAmound += itemValue;
  49. }
  50. }
  51.  
  52.  
  53. if (goldDict.Any())
  54. {
  55. PrintDictionary(goldDict, "Gold");
  56. if (gemDict.Any())
  57. {
  58. PrintDictionary(gemDict, "Gem");
  59. if (cashDict.Any())
  60. {
  61. PrintDictionary(cashDict, "Cash");
  62. }
  63. }
  64.  
  65. }
  66. }
  67.  
  68. private static void PrintDictionary(Dictionary<string, long> dict, string type)
  69. {
  70. Console.WriteLine($"<{type}> ${dict.Values.Sum()}");
  71. foreach (var item in dict.OrderByDescending(x => x.Key).ThenBy(x => x.Value))
  72. {
  73. Console.WriteLine($"##{item.Key} - {item.Value}");
  74. }
  75. }
  76.  
  77. private static bool IsValid(long itemValue, long currentAmound, long maxAmound)
  78. {
  79. long sumIsValid = itemValue + currentAmound;
  80. return (sumIsValid <= maxAmound);
  81. }
  82.  
  83. private static void NewItemInTheBag(Dictionary<string, long> dict, string item, long itemValue)
  84. {
  85. if (!dict.ContainsKey(item))
  86. {
  87. dict.Add(item, itemValue);
  88. }
  89. else
  90. {
  91. dict[item] += itemValue;
  92. }
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment