Submission #1329935

#TimeUsernameProblemLanguageResultExecution timeMemory
1329935HduongHacker (BOI15_hac)C++20
100 / 100
42 ms13120 KiB
#include <bits/stdc++.h>
#define task "task"

using namespace std;
const long long INF = 1e18;
const int N = 5e5 + 5;
long long n, a[N], sum[N], res[N];

long long get_sum (int l, int r) {
  long long res = 0;
  if (l <= r) res = sum[r] - sum[l];
  else res = sum[n] - sum[l] + sum[r];
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  if (fopen(task".inp", "r")) {
    freopen(task".inp", "r", stdin);
    freopen(task".out", "w", stdout);
  }

  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];

  if (n == 1) {
    cout << a[1];
    return 0;
  }

  long long siz = (n + 1) / 2;
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];

  for (int i = 0; i < n; i++) res[i] = get_sum(i, (i + siz) % n);

  deque <int> q = {0};
  long long ans = 0;
  for (int i = 1; i < siz; i++) {
    while(!q.empty() and res[i] < res[q.back()]) q.pop_back();
    q.push_back(i);
  }
//  for (int i = 0; i < n; i++) cout << res[i] << " ";
  for (int i = siz; i < siz + n; i++) {
    ans = max(ans, res[q.front()]);
    if ((i - siz) % n == q.front()) q.pop_front();
    while (!q.empty() and res[i % n] < res[q.back()]) q.pop_back();
    q.push_back(i % n);
  }
  cout << ans << "\n";
}


Compilation message (stderr)

hac.cpp: In function 'int main()':
hac.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen(task".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
hac.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen(task".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...