Cod sursa(job #2957634)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 23 decembrie 2022 10:14:53
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

template <typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template <typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
using i64 = long long int;

const int INF = INT_MAX, MOD = 1e9 + 7;
const long long LINF = 1e18;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {0, 0, 0, -1, 1, -1, 1, 1, -1};
const int dy[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  /// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  
  ifstream cin("secv2.in");
  ofstream cout("secv2.out");
  
  int N, K; cin >> N >> K;
  vector<i64> nums(N + 1);
  i64 ans = -LINF, sum_k = 0, sum_now = -LINF, left_now = 1, left_max = 1, right_max = K;
  for (int i = 1; i <= N; i++) {
    cin >> nums[i];
    sum_k += nums[i];
    if (i > K)
      sum_k -= nums[i - K];
    if (i >= K) {
      if (sum_k >= sum_now + nums[i]) {
        sum_now = sum_k;
        left_now = i - K + 1;
      } else {
        sum_now += nums[i];
      }

      if (sum_now > ans) {
        ans = sum_now;
        left_max = left_now;
        right_max = i;
      }
    }
  }

  cout << left_max << " " << right_max << " " << ans << "\n";

  return 0;
}