-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRPLC.cs
43 lines (39 loc) · 1.3 KB
/
RPLC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
// https://www.spoj.com/problems/RPLC/ #ad-hoc
// Finds the lowest possible starting energy level when drinking ordered +- cokes.
public static class RPLC
{
public static long Solve(int[] cokes)
{
// Assuming we start with zero energy, how low would we go?
long currentEnergyLevel = 0;
long lowestEnergyLevel = 0;
for (int i = 0; i < cokes.Length; ++i)
{
currentEnergyLevel += cokes[i];
if (currentEnergyLevel < lowestEnergyLevel)
{
lowestEnergyLevel = currentEnergyLevel;
}
}
return lowestEnergyLevel < 0
// E.g., if we got to -3, we would've dipped down to the min of 1 if we started with 4.
? -1 * lowestEnergyLevel + 1
// If we never got to a negative energy level, we still need 1 to begin the journey.
: 1;
}
}
public static class Program
{
private static void Main()
{
int testCount = int.Parse(Console.ReadLine());
for (int t = 1; t <= testCount; ++t)
{
int n = int.Parse(Console.ReadLine());
int[] cokes = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
Console.WriteLine(
$"Scenario #{t}: {RPLC.Solve(cokes)}");
}
}
}