Cod sursa(job #2930603)

Utilizator LukyenDracea Lucian Lukyen Data 28 octombrie 2022 21:14:04
Problema Buline Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;

#define vecLen 150

ifstream fin("buline.in");
ofstream fout("buline.out");

int vec[vecLen];
int main()
{
  cin.rdbuf(fin.rdbuf());
  cout.rdbuf(fout.rdbuf());

  int n, x, sign;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    cin >> x;
    cin >> sign;
    vec[i] = x;
    if (!sign)
      vec[i] *= -1;
  }

  tuple<int, int, int> cMax{-1, -1, INT_MIN}, tMax{-1, -1, INT_MIN};
  bool wrapped = false;
  for (int i = 0; i < 2 * n; i++)
  {
    if (i >= n)
      wrapped = true;

    if (wrapped && i % n == get<0>(cMax))
      break;

    if (vec[i % n] >= vec[i % n] + get<2>(cMax))
      get<0>(cMax) = i % n, get<1>(cMax) = i % n, get<2>(cMax) = vec[i % n];
    else
      get<1>(cMax) = i % n, get<2>(cMax) += vec[i % n];

    if (get<2>(cMax) > get<2>(tMax))
      tMax = cMax;
  }

  int len;
  if (get<1>(tMax) == get<0>(tMax))
    len = 1;
  else if (get<0>(tMax) < get<1>(tMax))
    len = get<1>(tMax) - get<0>(tMax) + 1;
  else
    len = (get<1>(tMax) + n) - get<0>(tMax) + 1;

  cout << get<2>(tMax) << " " << get<0>(tMax) + 1 << " " << len;

  return 0;
}