Guest User

Untitled

a guest
Feb 9th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem3.GreedyTimes
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. long bagCapacity = long.Parse(Console.ReadLine());
  12.  
  13. var items = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  14.  
  15. long goldSum = 0;
  16. var gems = new Dictionary<string, long>();
  17. long gemSum = 0;
  18. var cash = new Dictionary<string, long>();
  19. long cashSum = 0;
  20.  
  21. for (int searchItem = 0; searchItem < items.Length; searchItem += 2)
  22. {
  23. if (items[searchItem] == "Gold")
  24. {
  25. long goldAmount = long.Parse(items[searchItem + 1]);
  26.  
  27. if (bagCapacity >= goldAmount)
  28. {
  29. goldSum += goldAmount;
  30. bagCapacity -= goldAmount;
  31. }
  32. }
  33. else if (isGem(items[searchItem]))
  34. {
  35. long gemAmount = long.Parse(items[searchItem + 1]);
  36.  
  37. if (bagCapacity >= gemAmount && gemAmount + gemSum <= goldSum)
  38. {
  39. gemSum += gemAmount;
  40. bagCapacity -= gemAmount;
  41. if (!gems.ContainsKey(items[searchItem]))
  42. {
  43. gems.Add(items[searchItem], gemAmount);
  44. }
  45. else
  46. {
  47. gems[items[searchItem]] += gemAmount;
  48. }
  49. }
  50. }
  51. else if (items[searchItem].Length == 3)
  52. {
  53. long cashAmount = long.Parse(items[searchItem + 1]);
  54.  
  55. if (bagCapacity >= cashAmount && cashAmount + cashSum <= gemSum)
  56. {
  57. cashSum += cashAmount;
  58. bagCapacity -= cashAmount;
  59. if (!cash.ContainsKey(items[searchItem]))
  60. {
  61. cash.Add(items[searchItem], cashAmount);
  62. }
  63. else
  64. {
  65. cash[items[searchItem]] += cashAmount;
  66. }
  67. }
  68. }
  69. if (bagCapacity == 0)
  70. {
  71. break;
  72. }
  73. }
  74. if (goldSum != 0)
  75. {
  76. Console.WriteLine($"<Gold> ${goldSum}");
  77. Console.WriteLine($"##Gold - {goldSum}");
  78. }
  79. if (gemSum != 0)
  80. {
  81. Console.WriteLine($"<Gem> ${gemSum}");
  82. foreach (var gem in gems.OrderByDescending(x => x.Key).ThenBy(x => x.Value).Where(x => x.Value > 0))
  83. {
  84. Console.WriteLine($"##{gem.Key} - {gem.Value}");
  85. }
  86. }
  87. if (cashSum != 0)
  88. {
  89. Console.WriteLine($"<Cash> ${cashSum}");
  90. foreach (var item in cash.OrderByDescending(x => x.Key).ThenBy(x => x.Value).Where(x => x.Value > 0))
  91. {
  92. Console.WriteLine($"##{item.Key} - {item.Value}");
  93. }
  94. }
  95. }
  96.  
  97. private static bool isGem(string gem)
  98. {
  99. if (gem.Length >= 4)
  100. {
  101. var exist = gem.Substring(gem.Length - 3, 3).ToLower();
  102. if (exist == "gem")
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment