Advertisement
JosepRivaille

P20262: Sumes màximes

Apr 6th, 2015
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. //Pre: Llegeix seqüències d'n enters
  7. //Post: Escriu la màxima quantitat que es pot obtenir sumant des de cada costat
  8. int main() {
  9.     int n, x;
  10.     while (cin >> n) {
  11.         vector<int> maxs(n);
  12.         int maxed = 0; //Màxim esquerra-dreta
  13.         int sumed = 0; //Suma esquerra-dreta
  14.         int maxde = 0; //Màxim dreta-esquerra
  15.         int sumde = 0; //Suma dreta-esquerra
  16.         for (int i = 0; i < n; ++i) {
  17.             cin >> x;
  18.             maxs[i] = x;
  19.             sumed = sumed + x;
  20.             if (sumed > maxed) maxed = sumed;
  21.         }
  22.         for (int i = n-1; i >= 0; --i) {
  23.             sumde = sumde + maxs[i];
  24.             if (sumde > maxde) maxde = sumde;
  25.         }
  26.         cout << maxed << ' ' << maxde << endl;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement