-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-subarray-ii.cpp
61 lines (48 loc) · 1.06 KB
/
maximum-subarray-ii.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef vector<lli> vi;
typedef tuple<lli, lli> ii;
typedef tuple<lli, lli, lli> iii;
void task()
{
lli lenNumbers, minLength, maxLength;
cin >> lenNumbers >> minLength >> maxLength;
vi numbers(lenNumbers);
for (lli i = 0; i < lenNumbers; i++)
cin >> numbers[i];
lli currLength = 0;
lli currSum = 0;
lli bestSum = LONG_LONG_MIN;
for (lli i = 0; i < lenNumbers; i++)
{
lli num = numbers[i];
if (num > currSum + num)
{
currSum = num;
currLength = 1;
}
else
{
currSum += num;
currLength++;
}
if (currLength > maxLength)
{
currSum = num;
currLength = 1;
}
if (currLength >= minLength && currLength <= maxLength)
{
bestSum = max(currSum, bestSum);
}
}
cout<<bestSum<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
task();
return 0;
}