-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.cpp
More file actions
49 lines (38 loc) · 981 Bytes
/
test.cpp
File metadata and controls
49 lines (38 loc) · 981 Bytes
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
44
45
46
47
48
49
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N;
cin >> N;
// Create a vector to store the integers
vector<int> integers(N);
// Read the integers from the input
for (int i = 0; i < N; i++)
{
cin >> integers[i];
}
// Initialize the maximum sum
int maximumSum = integers[0];
// Maintain a current sum
int currentSum = integers[0];
// Iterate over the list
for (int i = 1; i < N; i++)
{
// Update the current sum
currentSum += integers[i];
// If the current sum is greater than the maximum sum, update the maximum sum
if (currentSum > maximumSum)
{
maximumSum = currentSum;
}
// If the current sum is less than zero, reset the current sum to zero
if (currentSum < 0)
{
currentSum = 0;
}
}
// Return the maximum sum
cout << maximumSum << endl;
return 0;
}