147912025-02-02 17:24:09sarminVásárlás (75 pont)cpp17Accepted 75/7520ms1624 KiB
// Created by Armin on 2/1/2025.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pp = pair<int, int>;


/*
3 2 -6 1 4 2 -9 11 3
3 5 -1 1 5 7 -2 11 14
sum
start
*/

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    
    int n; cin >> n;
    vector<int> a(n), b(n);
    int sumb = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<int> ba(n);
    for (int i = 0; i < n; i++) {
      cin >> b[i];
      sumb += b[i];
      ba[i] = b[i] - a[i];
      //cout << ba[i] << " ";
    }
    //cout << "\n";

    int start = 0, sum = 0;
    int maxstart = 0, maxend = -1, maxsum = 0;
    for (int i = 0; i < n; i++) {
      if (sum < 0) {
        start = i;
        sum = 0;
      }
      sum += ba[i];
      if (sum > maxsum) {
        maxsum = sum;
        maxstart = start;
        maxend = i;
      }
    }

    /*int i = 0, j = 0, sum = 0;
    int maxi = 0, maxj = -1, maxsum = 0;
    while (i < n && j < n) {
      if (sum + ba[j] < 0) {
        j++;
        i = j;
        sum = ba[i];
        if (sum > maxsum) {
          maxsum = sum;
          maxi = i;
          maxj = j;
        }
      } else {
        sum += ba[j];
        if (sum > maxsum) {
          maxsum = sum;
          maxi = i;
          maxj = j;
        }
        j++;
      }
      //cout << i << " " << j << " " << sum << "\n";
    }*/
    //cout << maxi << " " << maxj << " " << maxsum << "\n";
    int c = 0, d = 0;
    for (int k = maxstart; k <= maxend; k++) {
      c += a[k];
      d += b[k];
    }
    cout << c + sumb - d << '\n';
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base75/75
1Accepted0/01ms316 KiB
2Accepted0/09ms820 KiB
3Accepted3/31ms316 KiB
4Accepted4/41ms512 KiB
5Accepted4/41ms508 KiB
6Accepted4/41ms556 KiB
7Accepted4/41ms316 KiB
8Accepted3/31ms508 KiB
9Accepted4/41ms316 KiB
10Accepted4/41ms444 KiB
11Accepted4/41ms444 KiB
12Accepted5/51ms316 KiB
13Accepted3/319ms1588 KiB
14Accepted3/320ms1476 KiB
15Accepted3/319ms1476 KiB
16Accepted3/319ms1480 KiB
17Accepted3/319ms1588 KiB
18Accepted3/319ms1624 KiB
19Accepted3/319ms1588 KiB
20Accepted3/319ms1600 KiB
21Accepted3/319ms1480 KiB
22Accepted3/317ms1604 KiB
23Accepted3/320ms1588 KiB
24Accepted3/319ms1480 KiB