Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _2.SoftUni_Water_Supplies
- {
- class Program
- {
- static void Main(string[] args)
- {
- double totalWater = double.Parse(Console.ReadLine());
- double[] bottles = Console.ReadLine().Split().Select(double.Parse).ToArray();
- double capacity = double.Parse(Console.ReadLine());
- double bottleCapacity = 0;
- double waterSpent = 0;
- double totalWaterSpent = 0;
- int bottlesLeft = 0;
- int insufficiencyPoint = -1;
- double littersNeeded = 0;
- bool even = (totalWater % 2 == 0);
- if (totalWater % 2 == 0)
- {
- for (int bottle = 0; bottle < bottles.Length; bottle++)
- {
- bottleCapacity = bottles[bottle];
- if (capacity - bottleCapacity <= totalWater)
- {
- waterSpent = capacity - bottleCapacity;
- totalWaterSpent += waterSpent;
- bottles[bottle] += waterSpent;
- totalWater -= waterSpent;
- }
- else
- {
- insufficiencyPoint = bottle;
- waterSpent = totalWater;
- totalWaterSpent += waterSpent;
- bottles[bottle] += waterSpent;
- totalWater = 0;
- bottlesLeft = bottles.Length - bottle;
- for (int halfFull = insufficiencyPoint; halfFull < insufficiencyPoint + bottlesLeft; halfFull++)
- {
- bottleCapacity = bottles[halfFull];
- littersNeeded += capacity - bottleCapacity;
- }
- break;
- }
- } // end of for
- } //end of if
- else
- {
- for (int bottle = bottles.Length - 1; bottle >= 0; bottle--)
- {
- bottleCapacity = bottles[bottle];
- if (capacity - bottleCapacity <= totalWater)
- {
- waterSpent = capacity - bottleCapacity;
- totalWaterSpent += waterSpent;
- bottles[bottle] += waterSpent;
- totalWater -= waterSpent;
- }
- else
- {
- insufficiencyPoint = bottle;
- waterSpent = totalWater;
- totalWaterSpent += waterSpent;
- bottles[bottle] += waterSpent;
- totalWater = 0;
- bottlesLeft = bottle + 1;
- for (int halfFull = insufficiencyPoint; halfFull >= 0; halfFull--)
- {
- bottleCapacity = bottles[halfFull];
- littersNeeded += capacity - bottleCapacity;
- }
- break;
- }
- }//end of for
- }//end of else
- int[] remainingBottlesIndexes = new int[bottlesLeft];
- if (insufficiencyPoint != -1 && even)
- {
- int k = 0;
- for (int bottle = bottles.Length - 1; bottle >= insufficiencyPoint; bottle--)
- {
- remainingBottlesIndexes[k] = bottle;
- k++;
- }
- remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
- }
- else if (insufficiencyPoint != -1 && !even)
- {
- for (int bottle = 0; bottle < bottlesLeft; bottle++)
- {
- remainingBottlesIndexes[bottle] = bottle;
- }
- remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
- }
- if (bottlesLeft == 0)
- {
- Console.WriteLine("Enough water!");
- Console.WriteLine($"Water left: {totalWater}l.");
- }
- else
- {
- Console.WriteLine("We need more water!");
- Console.WriteLine($"Bottles left: {bottlesLeft}");
- Console.WriteLine("With indexes: " + string.Join(", ", remainingBottlesIndexes)); ;
- Console.WriteLine($"We need {littersNeeded} more liters!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement