fork download
  1. //Ненавижу 1С для mista.ru
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp7
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var result = 0;
  15. var s = Console.ReadLine();
  16. var builds = s.Split(' ').Select(c => Int32.Parse(c)).ToList();
  17. int last = builds.Count()-1;
  18. int based = 0;
  19. while(based!=last)
  20. {
  21. var nextBased = based;
  22. var maxIndex = based+1;
  23. for (int i=based+1;i<=last;i++)
  24. {
  25. if(builds[i]>builds[based])
  26. {
  27. nextBased = i;
  28. break;
  29. }
  30. else if(builds[i]>=builds[maxIndex])
  31. {
  32. maxIndex = i;
  33. }
  34. }
  35. if(nextBased==based)
  36. {
  37. nextBased = maxIndex;
  38. }
  39. var level = Math.Min(builds[based], builds[nextBased]);
  40. for(int i=based+1;i<nextBased;i++)
  41. {
  42. result += (level - builds[i]);
  43. }
  44. based = nextBased;
  45. }
  46. Console.WriteLine(result);
  47. Console.ReadKey();
  48. }
  49. }
  50. }
  51.  
Success #stdin #stdout 0.04s 17432KB
stdin
1 0 5 0 5 0 3 0 1
stdout
10