-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.cpp
More file actions
34 lines (28 loc) · 850 Bytes
/
sum.cpp
File metadata and controls
34 lines (28 loc) · 850 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
/*
Algoritimo soma maxima de elementos contiguos tempo O(n) espaço O(1)
Codigo testado em: https://www.urionlinejudge.com.br/judge/en/problems/view/2917
*/
#include <bits/stdc++.h>
using namespace std;
int vet[1000000];
int somaMaxima(int n){
int maximo = 0, maximo_global = 0, ini = n;
for(int i = 0; i < n * 2 && i % n != ini; i++){
if(maximo + vet[i % n] <= vet[i % n]){
maximo = vet[i % n];
ini = i;//Tomar cuidado para não somar numeros repitdos
}else
maximo += vet[i % n];
maximo_global = max(maximo_global, maximo);
}
return maximo_global;
}
int main(){
int n;
while(scanf(" %d", &n) != EOF){
for(int i = 0; i < n * 10; i++)
scanf(" %d", vet + i);
cout << somaMaxima(n * 10) << endl;
}
return 0;
}