Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pii pair<int, int>
- #define vi vector<int>
- #define ll long long
- #define pb push_back
- #define f first
- #define s second
- using namespace std;
- int dp(int i, int n, vi &val, vi &tabela) {
- if(i == 0)
- return tabela[i] = val[0];
- if(tabela[i] != (int)-1e9)
- return tabela[i];
- return tabela[i] = max(val[i],dp(i-1, n, val, tabela)+val[i]);
- }
- int main() {
- int n;
- scanf("%d", &n);
- vi tabela1(n, -1e9);
- vi somaorig(n);
- vi soma(n);
- vi maximos(n);
- vi val(n);
- for(int i = 0; i < n; i++) {
- scanf("%d", &val[i]);
- }
- somaorig[0] = val[0];
- for(int i = 1; i < n; i++) {
- somaorig[i] = somaorig[i-1] + val[i];
- }
- dp(n-1, n, val, tabela1);
- soma[n-1] = val[n-1];
- for(int i = n-2; i >= 0; i--) {
- soma[i] = val[i] + soma[i+1];
- }
- maximos[n-1] = val[n-1];
- int maior = maximos[n-1];
- for(int i = n-2; i >= 0; i--) {
- maior = max(soma[i], maior);
- maximos[i] = maior;
- }
- maior = tabela1[n-1];
- for(int i = n-2; i >= 0; i--) {
- maior = max(maior, max(tabela1[i], somaorig[i] + maximos[i+1]));
- }
- if(maior >= 0)
- cout << maior << endl;
- else
- cout << "0" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement