View difference between Paste ID: p7vDDB1a and MM0bkR5d
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
namespace _2.SoftUni_Water_Supplies
8
{
9
    class Program
10
    {
11
        static void Main(string[] args)
12
        {
13
            double totalWater = double.Parse(Console.ReadLine());
14
            double[] bottles = Console.ReadLine().Split().Select(double.Parse).ToArray();
15
            double capacity = double.Parse(Console.ReadLine());
16
17
            double bottleCapacity = 0;
18
            double waterSpent = 0;
19
            double totalWaterSpent = 0;
20
            int bottlesLeft = 0;
21
            int insufficiencyPoint = -1;
22
            double littersNeeded = 0;
23
            bool even = (totalWater % 2 == 0);
24
25
            if (totalWater % 2 == 0)
26
            {
27
                for (int bottle = 0; bottle < bottles.Length; bottle++)
28
                {
29
                    bottleCapacity = bottles[bottle];
30
                    if (capacity - bottleCapacity <= totalWater)
31
                    {
32
                        waterSpent = capacity - bottleCapacity;
33
                        totalWaterSpent += waterSpent;
34
                        bottles[bottle] += waterSpent;
35
                        totalWater -= waterSpent;
36
                    }
37
                    else
38
                    {
39
                        insufficiencyPoint = bottle;
40
                        waterSpent = totalWater;
41
                        totalWaterSpent += waterSpent;
42
                        bottles[bottle] += waterSpent;
43
                        totalWater = 0;
44
                        bottlesLeft = bottles.Length - bottle;
45
                        for (int halfFull = insufficiencyPoint; halfFull < insufficiencyPoint + bottlesLeft; halfFull++)
46
                        {
47
                            bottleCapacity = bottles[halfFull];
48
                            littersNeeded += capacity - bottleCapacity;
49
                        }
50
                        break;
51
                    }
52
                } // end of for
53
            } //end of if
54
55
            else
56
            {
57
                for (int bottle = bottles.Length - 1; bottle >= 0; bottle--)
58
                {
59
                    bottleCapacity = bottles[bottle];
60
                    if (capacity - bottleCapacity <= totalWater)
61
                    {
62
                        waterSpent = capacity - bottleCapacity;
63
                        totalWaterSpent += waterSpent;
64
                        bottles[bottle] += waterSpent;
65
                        totalWater -= waterSpent;
66
                    }
67
                    else
68
                    {
69
                        insufficiencyPoint = bottle;
70
                        waterSpent = totalWater;
71
                        totalWaterSpent += waterSpent;
72
                        bottles[bottle] += waterSpent;
73
                        totalWater = 0;
74
                        bottlesLeft = bottle + 1;
75
                        for (int halfFull = insufficiencyPoint; halfFull >= 0; halfFull--)
76
                        {
77
                            bottleCapacity = bottles[halfFull];
78
                            littersNeeded += capacity - bottleCapacity;
79
                        }
80
                        break;
81
                    }
82
                }//end of for
83
            }//end of else
84
85
            int[] remainingBottlesIndexes = new int[bottlesLeft];
86
87
            if (insufficiencyPoint != -1 && even)
88
            {
89
                int k = 0;
90
                for (int bottle = bottles.Length - 1; bottle >= insufficiencyPoint; bottle--)
91
                {
92
                    remainingBottlesIndexes[k] = bottle;
93
                    k++;
94
                }
95
                remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
96
            }
97
            else if (insufficiencyPoint != -1 && !even)
98
            {
99
100
                for (int bottle = 0; bottle < bottlesLeft; bottle++)
101
                {
102
                    remainingBottlesIndexes[bottle] = bottle;
103
                }
104
105
                remainingBottlesIndexes = remainingBottlesIndexes.Reverse().ToArray();
106
            }
107
108
109
            if (bottlesLeft == 0)
110
            {
111
                Console.WriteLine("Enough water!");
112
                Console.WriteLine($"Water left: {totalWater}l.");
113
            }
114
            else
115
            {
116
                Console.WriteLine("We need more water!");
117
                Console.WriteLine($"Bottles left: {bottlesLeft}");
118
                Console.WriteLine("With indexes: " + string.Join(", ", remainingBottlesIndexes)); ;
119
                Console.WriteLine($"We need {littersNeeded} more liters!");
120
            }
121
        }
122
    }
123
}