prithivMLmods/Coder-Stat huggingface dataset:
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n, k;
  std::cin >> n >> k;
  std::string s;
  std::cin >> s;
  std::vector<int> unhappy;
  if (s[0] == 'L')
    unhappy.emplace_back(1);
  if (s[n - 1] == 'R')
    unhappy.emplace_back(1);
  rep(i, n - 1) {
    if (s[i] == 'R' && s[i + 1] == 'L')
      unhappy.emplace_back(2);
  }
  std::sort(all(unhappy), std::greater<int>());
  int sum = n;
  for (int i : unhappy)
    sum -= i;
  rep(i, k) sum += unhappy[i];
  print(sum);

  return 0;
}


Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
  int n, k;
  std::cin >> n >> k;
  std::string s;
  std::cin >> s;
  std::vector<int> unhappy;
  if (s[0] == 'L')
    unhappy.emplace_back(1);
  if (s[n - 1] == 'R')
    unhappy.emplace_back(1);
  rep(i, n - 1) {
    if (s[i] == 'R' && s[i + 1] == 'L')
      unhappy.emplace_back(2);
  }
  std::sort(all(unhappy), std::greater<int>());
  int sum = n;
  for (int i : unhappy)
    sum -= i;
  rep(i, k) if (i < unhappy.size()) sum += unhappy[i];
  if (k >= unhappy.size())
    sum = n - 1;
  print(sum);

  return 0;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int score = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == s.at(i + 1))
      score++;
  }
  int ans = min(score + 2 * k, n - 1);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int score = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s.at(i) == s.at(i + 1))
      score++;
  }
  int ans = min(score + 2 * k, n - 1);
  cout << ans << endl;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  string s;
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == s[i + 1]) {
      ans++;
    }
  }
  cout << min(ans + 2 * k, n - 1) << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == s[i + 1]) {
      ans++;
    }
  }
  cout << min(ans + 2 * k, n - 1) << endl;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI pair<int, int>
const int maxm = 1e6 + 5;
char s[maxm];
int n, k;
signed main() {
  cin >> n >> k;
  scanf("%s", s + 1);
  int ans = 0;
  vector<int> rl, lr;
  for (int i = 2; i <= n; i++) {
    if (s[i] == s[i - 1]) {
      ans++;
    } else {
      if (s[i] == 'L') {
        rl.push_back(i);
      } else {
        lr.push_back(i);
      }
    }
  }
  int len1 = rl.size();
  int len2 = lr.size();
  int c = 0, cc = 0;
  vector<int> temp;
  while (c < len1 && cc < len2 && k) {
    int x = rl[c], y = lr[cc];
    if (x < y) { // R|LLL|R
      ans += 2;
      k--;
      cc++;
      while (rl[c] < y)
        c++;
    } else {
      temp.push_back(y);
      cc++;
    }
  }
  while (c < len1)
    temp.push_back(rl[c++]);
  while (cc < len2)
    temp.push_back(lr[cc++]);
  ans += min((int)temp.size(), k);
  cout << ans << endl;
  return 0;
}
/*
LLL这种全部一样的翻转之后权值不变,
只有R|LLL这种,翻转一遍之后会增加一个权值,
遇到R|LLL|R这种能增加两个权值.
其他情况下不增加了.
发现一次操作最多增加两个权值.
那么贪心一下就行了:
记录每个L|R和R|L的位置,然后贪心的翻转.
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI pair<int, int>
const int maxm = 1e6 + 5;
char s[maxm];
int n, k;
signed main() {
  cin >> n >> k;
  scanf("%s", s + 1);
  int ans = 0;
  vector<int> rl, lr;
  for (int i = 2; i <= n; i++) {
    if (s[i] == s[i - 1]) {
      ans++;
    } else {
      if (s[i] == 'L') {
        rl.push_back(i);
      } else {
        lr.push_back(i);
      }
    }
  }
  int len1 = rl.size();
  int len2 = lr.size();
  int c = 0, cc = 0;
  vector<int> temp;
  while (c < len1 && cc < len2 && k) {
    int x = rl[c], y = lr[cc];
    if (x < y) { // R|LLL|R
      ans += 2;
      k--;
      c++, cc++;
    } else { // L|RRR|L
      ans += 2;
      k--;
      c++, cc++;
    }
  }
  while (c < len1)
    temp.push_back(rl[c++]);
  while (cc < len2)
    temp.push_back(lr[cc++]);
  ans += min((int)temp.size(), k);
  cout << ans << endl;
  return 0;
}
/*
LLL这种全部一样的翻转之后权值不变,
只有R|LLL这种,翻转一遍之后会增加一个权值,
遇到R|LLL|R这种能增加两个权值.
其他情况下不增加了.
发现一次操作最多增加两个权值.
那么贪心一下就行了:
记录每个L|R和R|L的位置,然后贪心的翻转.
*/
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  11121
  114

  1211121121
  41121121
  621121
  9121

  55
  10

  3111112
  51112
  514
  }*/

signed main() {
  int n, k;
  string s;
  cin >> n >> k >> s;

  vi a;
  int cnt = 1;
  char cur = s[0];
  rep(i, 1, n) {
    if (cur == s[i]) {
      cnt++;
    } else {
      a.emplace_back(cnt);
      cnt = 1;
    }
    cur = s[i];
  }
  if (cnt)
    a.emplace_back(cnt);

  int len = 2 * k + 1, ans;
  if (sz(a) <= len) {
    int sum = 0;
    for (int i : a)
      sum += i;
    ans = sum - 1;
  } else {
    vi s(n + 1), s2(sz(a) + 1);
    rep(i, 0, n) s[i + 1] = s[i] + a[i];
    rep(i, 0, sz(a)) s2[i + 1] = s2[i] + a[i] - 1;

    ans = 0;
    rep(i, 0, sz(a) - len + 1) {
      int tmp = s[i + len] - s[i] - 1 + s2[sz(a)] - s2[i + len] + s2[i];
      //      cout << i << ":" << tmp << endl;
      chmax(ans, tmp);
    }
  }

  cout << ans << endl;

  //  rep(i, 0, sz(a)) cout << a[i] << " ";
  //  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL << 60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  11121
  114

  1211121121
  41121121
  621121
  9121

  55
  10

  3111112
  51112
  514
  }*/

signed main() {
  int n, k;
  string s;
  cin >> n >> k >> s;

  vi a;
  int cnt = 1;
  char cur = s[0];
  rep(i, 1, n) {
    if (cur == s[i]) {
      cnt++;
    } else {
      a.emplace_back(cnt);
      cnt = 1;
    }
    cur = s[i];
  }
  if (cnt)
    a.emplace_back(cnt);

  int len = 2 * k + 1, ans;
  if (sz(a) <= len) {
    int sum = 0;
    for (int i : a)
      sum += i;
    ans = sum - 1;
  } else {
    vi s(sz(a) + 1), s2(sz(a) + 1);
    rep(i, 0, sz(a)) s[i + 1] = s[i] + a[i];
    rep(i, 0, sz(a)) s2[i + 1] = s2[i] + a[i] - 1;

    ans = 0;
    rep(i, 0, sz(a) - len + 1) {
      int tmp = s[i + len] - s[i] - 1 + s2[sz(a)] - s2[i + len] + s2[i];
      //      cout << i << ":" << tmp << endl;
      chmax(ans, tmp);
    }
  }

  cout << ans << endl;

  //  rep(i, 0, sz(a)) cout << a[i] << " ";
  //  cout << endl;

  return 0;
}
Problem p02918 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;
  char x = s.at(0);
  int st = 1;
  int en = 0;
  for (int i = 0; i < k; i++) {
    en = s.length();
    for (int j = st - 1; j < s.length(); j++) {
      if (s.at(j) == x)
        continue;
      else {
        st = j;
        for (int k = j; k < s.length(); k++) {
          if (s.at(st) != s.at(k)) {
            en = k;
            break;
          }
        }
        break;
      }
    }
    for (int j = st; j < en; j++)
      s.at(j) = x;
  }
  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.at(i) == 'L') {
      if (i == 0)
        continue;
      else {
        if (s.at(i - 1) == 'L')
          ans++;
      }
    } else {
      if (i == s.length() - 1)
        continue;
      else {
        if (s.at(i + 1) == 'R')
          ans++;
      }
    }
  }
  // cout << s << endl;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;
  char x = s.at(0);
  int st = 1;
  int en = 0;
  for (int i = 0; i < k; i++) {
    en = s.length();
    for (int j = st - 1; j < s.length(); j++) {
      if (s.at(j) == x)
        continue;
      else {
        st = j;
        for (int k = j; k < s.length(); k++) {
          if (s.at(st) != s.at(k)) {
            en = k;
            break;
          }
        }
        break;
      }
    }
    for (int j = st; j < en; j++)
      s.at(j) = x;
    st = en - 1;
  }
  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s.at(i) == 'L') {
      if (i == 0)
        continue;
      else {
        if (s.at(i - 1) == 'L')
          ans++;
      }
    } else {
      if (i == s.length() - 1)
        continue;
      else {
        if (s.at(i + 1) == 'R')
          ans++;
      }
    }
  }
  // cout << s << endl;
  cout << ans << endl;
}
Problem p02918 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  string S;
  cin >> N >> K >> S;

  ll t = 0;
  for (ll i = 0; i < K; i++) {
    for (ll j = t; j < N; j++) {
      if (S[0] != S[j] && S[j] == S[j + 1]) {
        S[j] = S[0];
      } else if (S[0] != S[j]) {
        S[j] = S[0];
        t = j + 1;
        break;
      }
    }
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (i != 0 && S[i] == 'L' && S[i - 1] == 'L') {
      ans++;
    }
    if (i != N - 1 && S[i] == 'R' && S[i + 1] == 'R') {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ll N, K;
  string S;
  cin >> N >> K >> S;

  ll t = 0;
  for (ll i = 0; i < K; i++) {
    for (ll j = t; j < N; j++) {
      if (S[0] != S[j] && S[j] == S[j + 1]) {
        S[j] = S[0];
      } else if (S[0] != S[j]) {
        S[j] = S[0];
        t = j + 1;
        break;
      } else {
        t = j + 1;
      }
    }
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    if (i != 0 && S[i] == 'L' && S[i - 1] == 'L') {
      ans++;
    }
    if (i != N - 1 && S[i] == 'R' && S[i + 1] == 'R') {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define inf 999999999999
#define N 500009
#define M (L + R) / 2
#define ll long long
#define pb push_back
using namespace std;

ll n, k, ans, len, KL, KR;
string A, L, R;

ll check(string x) {
  ll pas = 0;
  ll nn = x.size();
  for (int i = 0; i < nn - 1; i++) {
    if (x[i] == 'R' && x[i + 1] == 'R')
      pas++;
  }
  for (int i = nn; i > 0; i--) {
    if (x[i] == 'L' && x[i - 1] == 'L')
      pas++;
  }
  return pas;
}

vector<pair<ll, pair<ll, ll>>> vL, vR;

int main() {

  ios::sync_with_stdio(0);

  cin >> n >> k;

  cin >> A;
  ans = check(A);

  L = A;
  L += '*';
  len = 0;

  for (int i = 0; i < n; i++) {
    if (i > 0 && L[i] == 'L' && L[i - 1] == 'R' && L[i + 1] == 'R') {
      vL.pb({5, {i, i}});
      len = 0;
      continue;
    }
    if (L[i] == 'L')
      len++;
    else
      continue;
    if (L[i] == 'L' && L[i + 1] != 'L') {
      vL.pb({len * 2, {i - len + 1, i}});
      len = 0;
    }
  }

  R = A;
  R += '*';
  len = 0;

  for (int i = 0; i < n; i++) {
    if (i > 0 && R[i] == 'R' && R[i - 1] == 'L' && R[i + 1] == 'L') {
      vR.pb({5, {i, i}});
      len = 0;
      continue;
    }
    if (R[i] == 'R')
      len++;
    else
      continue;
    if (R[i] == 'R' && R[i + 1] != 'R') {
      vR.pb({len * 2, {i - len + 1, i}});
      len = 0;
    }
  }

  sort(vL.begin(), vL.end());

  sort(vR.begin(), vR.end());
  /*
  for(int i=0;i<vL.size();i++){
          cout<<vL[i].f<<" "<<vL[i].s.f<<" - "<<vL[i].s.s<<endl;
  }
  */
  L = A;
  KL = k;
  int TRY = vL.size();
  while (KL--) {
    TRY--;
    for (int i = vL[TRY].s.f; i <= vL[TRY].s.s; i++) {
      L[i] = 'R';
    }
    if (KL < 100)
      ans = max(ans, check(L)); // cout<<L<<endl;
  }

  R = A;
  KR = k;
  TRY = vR.size();
  while (KR--) {
    TRY--;
    for (int i = vR[TRY].s.f; i <= vR[TRY].s.s; i++) {
      R[i] = 'L';
    }
    if (KR < 100)
      ans = max(ans, check(R));
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define inf 999999999999
#define N 500009
#define M (L + R) / 2
#define ll long long
#define pb push_back
using namespace std;

ll n, k, ans, len, KL, KR;
string A, L, R;

ll check(string x) {
  ll pas = 0;
  ll nn = x.size();
  for (int i = 0; i < nn - 1; i++) {
    if (x[i] == 'R' && x[i + 1] == 'R')
      pas++;
  }
  for (int i = nn; i > 0; i--) {
    if (x[i] == 'L' && x[i - 1] == 'L')
      pas++;
  }
  return pas;
}

vector<pair<ll, pair<ll, ll>>> vL, vR;

int main() {

  ios::sync_with_stdio(0);

  cin >> n >> k;

  cin >> A;
  ans = check(A);
  ans = min(n - 1, ans + 2 * k);

  cout << ans << endl;
  return 0;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
using namespace std;
int min(int a, int b);
int main() {
  char queue[10000];
  int happy_num = 0;
  int len, time;
  scanf("%d%d", &len, &time);
  scanf("%s", queue);
  for (int i = 0; i < strlen(queue); i++) {
    if ((i != strlen(queue) - 1) && (queue[i + 1] == 'R') && (queue[i] == 'R'))
      happy_num++;
    else if ((i != 0) && (queue[i - 1] == 'L') && (queue[i] == 'L'))
      happy_num++;
  }
  printf("%d", min((happy_num + 2 * time), len - 1));
  return 0;
}
int min(int a, int b) { return (a >= b) ? b : a; }

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
int min(int a, int b);
int main() {
  char queue[100000];
  int happy_num = 0;
  int len, time;
  scanf("%d%d", &len, &time);
  scanf("%s", queue);
  for (int i = 0; i < strlen(queue); i++) {
    if ((i != strlen(queue) - 1) && (queue[i + 1] == 'R') && (queue[i] == 'R'))
      happy_num++;
    else if ((i != 0) && (queue[i - 1] == 'L') && (queue[i] == 'L'))
      happy_num++;
  }
  printf("%d", min((happy_num + 2 * time), len - 1));
  return 0;
}
int min(int a, int b) { return (a >= b) ? b : a; }
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> pos;
  char cur = 'a';
  for (int i = 0; i < N; i++) {
    if (cur != S[i]) {
      cur = S[i];
      pos.emplace_back(i);
    }
  }
  pos.emplace_back(N);
  cur = S[0];
  for (int i = 1; i < (int)pos.size() && K > 0; i += 2) {
    for (int j = pos[i]; j < pos[i + 1]; j++) {
      S[j] = cur;
    }
    K--;
  }
  int res = 0;
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == S[i + 1])
      res++;
  }
  cout << res << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;
  vector<int> pos;
  char cur = 'a';
  for (int i = 0; i < N; i++) {
    if (cur != S[i]) {
      cur = S[i];
      pos.emplace_back(i);
    }
  }
  pos.emplace_back(N);
  cur = S[0];
  for (int i = 1; i < (int)pos.size() - 1 && K > 0; i += 2) {
    for (int j = pos[i]; j < pos[i + 1]; j++) {
      S[j] = cur;
    }
    K--;
  }
  int res = 0;
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == S[i + 1])
      res++;
  }
  cout << res << '\n';
  return 0;
}
Problem p02918 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define ll long long

using namespace std;

const int N = 110;
const int mod = 10007;

inline int read() {
  int jg = 0, jk = getchar() - '0', f = 1;
  while (jk < 0 || jk > 9) {
    if (jk == '-' - '0')
      f = -1;
    jk = getchar() - '0';
  }
  while (jk >= 0 && jk <= 9)
    jg *= 10, jg += jk, jk = getchar() - '0';
  return jg * f;
}

int n, ans, k;
char s[N];

int main() {
  // freopen("a.txt", "r", stdin);
  n = read(), k = read();
  scanf("%s", s);
  for (int i = 1; i < n; ++i)
    if (s[i] == s[i - 1])
      ans++;
  ans += 2 * k;
  ans = min(ans, n - 1);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

#define ll long long

using namespace std;

const int N = 100010;
const int mod = 10007;

inline int read() {
  int jg = 0, jk = getchar() - '0', f = 1;
  while (jk < 0 || jk > 9) {
    if (jk == '-' - '0')
      f = -1;
    jk = getchar() - '0';
  }
  while (jk >= 0 && jk <= 9)
    jg *= 10, jg += jk, jk = getchar() - '0';
  return jg * f;
}

int n, ans, k;
char s[N];

int main() {
  // freopen("a.txt", "r", stdin);
  n = read(), k = read();
  scanf("%s", s);
  for (int i = 1; i < n; ++i)
    if (s[i] == s[i - 1])
      ans++;
  ans += 2 * k;
  ans = min(ans, n - 1);
  printf("%d", ans);
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> p(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i].first, p[i].second = i;
  sort(p.begin(), p.end(), greater<>());
  set<int> se;
  se.insert(-1);
  se.insert(n);
  se.insert(p[0].second);
  long long ans = 0;
  for (int i = 1; i < n; ++i) {
    int now = p[i].second;
    auto r = lower_bound(se.begin(), se.end(), now);
    auto l = r;
    l--;
    long long lcnt1 = now - *l, rcnt1 = *r - now;
    long long lcnt2 = 0, rcnt2 = 0;
    if (l != se.begin())
      l--, lcnt2 = now - *l - lcnt1;
    r++;
    if (r != se.end())
      rcnt2 = *r - now - rcnt1;
    ans += p[i].first * (lcnt1 * rcnt2 + rcnt1 * lcnt2);
    se.insert(now);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> p(n);
  for (int i = 0; i < n; ++i)
    cin >> p[i].first, p[i].second = i;
  sort(p.begin(), p.end(), greater<>());
  set<int> se;
  se.insert(-1);
  se.insert(n);
  se.insert(p[0].second);
  long long ans = 0;
  for (int i = 1; i < n; ++i) {
    int now = p[i].second;
    auto r = se.lower_bound(now);
    auto l = r;
    l--;
    long long lcnt1 = now - *l, rcnt1 = *r - now;
    long long lcnt2 = 0, rcnt2 = 0;
    if (l != se.begin())
      l--, lcnt2 = now - *l - lcnt1;
    r++;
    if (r != se.end())
      rcnt2 = *r - now - rcnt1;
    ans += p[i].first * (lcnt1 * rcnt2 + rcnt1 * lcnt2);
    se.insert(now);
  }
  cout << ans << endl;

  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll po[222222];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vi A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    po[A[i]] = i + 1;
  }
  set<ll> se;
  se.insert(po[n]);
  se.insert(0);
  se.insert(n + 1);
  // for(auto a:se)cout<<a <<" ";
  // cout<<endl;
  ll ans = 0;
  for (ll i = n - 1; i >= 1; i--) {
    auto lb = lower_bound(ALL(se), po[i]);
    ll num = *lb;
    --lb;
    ll num2 = *lb;
    if (num == n + 1) {
      --lb;
      ll num3 = *lb;
      ans += (num - po[i]) * (num2 - num3) * i;
    } else if (num2 == 0) {
      // auto lb3=lower_bound(ALL(se),num+1);
      ++lb;
      ++lb;
      ll num3 = *lb;
      ans += po[i] * (num3 - num) * i;
    } else {
      --lb;
      ll num4 = *lb;
      ++lb;
      ++lb;
      ++lb;
      // auto lb3=lower_bound(ALL(se),num+1);
      ll num3 = *lb;
      ans += (po[i] - num2) * (num3 - num) * i;

      ans += (num - po[i]) * (num2 - num4) * i;
    }
    se.insert(po[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr << " " << (hoge) << endl;
#define DBG cerr << "!" << endl;
#define BITLE(n) (1LL << ((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s, f, t) ((s).substr((f)-1, (t) - (f) + 1))
#define ALL(a) (a).begin(), (a).end()
using namespace std;
ll po[222222];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vi A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    po[A[i]] = i + 1;
  }
  set<ll> se;
  se.insert(po[n]);
  se.insert(0);
  se.insert(n + 1);
  // for(auto a:se)cout<<a <<" ";
  // cout<<endl;
  ll ans = 0;
  for (ll i = n - 1; i >= 1; i--) {
    auto lb = se.lower_bound(po[i]);
    ll num = *lb;
    --lb;
    ll num2 = *lb;
    if (num == n + 1) {
      --lb;
      ll num3 = *lb;
      ans += (num - po[i]) * (num2 - num3) * i;
    } else if (num2 == 0) {
      // auto lb3=lower_bound(ALL(se),num+1);
      ++lb;
      ++lb;
      ll num3 = *lb;
      ans += po[i] * (num3 - num) * i;
    } else {
      --lb;
      ll num4 = *lb;
      ++lb;
      ++lb;
      ++lb;
      // auto lb3=lower_bound(ALL(se),num+1);
      ll num3 = *lb;
      ans += (po[i] - num2) * (num3 - num) * i;

      ans += (num - po[i]) * (num2 - num4) * i;
    }
    se.insert(po[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, s, e) for (int i = (s); i <= (e); ++i)
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define FAST_IO()                                                              \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto x : v)
    os << x << " ";
  return os << "\n";
}
template <class U, class V>
ostream &operator<<(ostream &os, const pair<U, V> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}

int main() {
  FAST_IO();
  int N;
  cin >> N;
  vector<int> P(N + 1);
  repp(i, 1, N) {
    int x;
    cin >> x;
    P[x] = i;
  }
  set<int> bigger;
  bigger.insert(0);
  bigger.insert(N + 1);
  ll ans = 0;
  for (int x = N; x >= 1; --x) {
    auto pos = lower_bound(all(bigger), P[x]);
    int R = *(pos);
    int RR;
    if (R == N + 1)
      RR = R;
    else {
      RR = *(++pos);
      --pos;
    }
    int L = *(--pos);
    int LL;
    if (L == 0)
      LL = L;
    else {
      LL = *(--pos);
    }
    ans += (ll)x * ((L - LL) * (R - P[x]) + (RR - R) * (P[x] - L));
    bigger.insert(P[x]);
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, s, e) for (int i = (s); i <= (e); ++i)
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define FAST_IO()                                                              \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto x : v)
    os << x << " ";
  return os << "\n";
}
template <class U, class V>
ostream &operator<<(ostream &os, const pair<U, V> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}

int main() {
  FAST_IO();
  int N;
  cin >> N;
  vector<int> P(N + 1);
  repp(i, 1, N) {
    int x;
    cin >> x;
    P[x] = i;
  }
  set<int> bigger;
  bigger.insert(0);
  bigger.insert(N + 1);
  ll ans = 0;
  for (int x = N; x >= 1; --x) {
    auto pos = bigger.lower_bound(P[x]);
    int R = *(pos);
    int RR;
    if (R == N + 1)
      RR = R;
    else {
      RR = *(++pos);
      --pos;
    }
    int L = *(--pos);
    int LL;
    if (L == 0)
      LL = L;
    else {
      LL = *(--pos);
    }
    ans += (ll)x * ((L - LL) * (R - P[x]) + (RR - R) * (P[x] - L));
    bigger.insert(P[x]);
  }
  cout << ans << "\n";
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> idx(n + 1);
  for (int i = 1; i <= n; ++i) {
    int p;
    cin >> p;
    idx[p] = i;
  }

  multiset<int> mset{0, 0, n + 1, n + 1};
  ll ans = 0;
  for (int i = n; i >= 1; --i) {
    int c = idx[i];
    auto it_d = lower_bound(mset.begin(), mset.end(), c);
    ll d = *it_d;
    ll e = *(++it_d);
    ll b = *(-- --it_d);
    ll a = *(--it_d);

    ans += ((e - d) * (c - b) + (b - a) * (d - c)) * (ll)i;

    mset.insert(c);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> idx(n + 1);
  for (int i = 1; i <= n; ++i) {
    int p;
    cin >> p;
    idx[p] = i;
  }

  multiset<int> mset{0, 0, n + 1, n + 1};
  ll ans = 0;
  for (int i = n; i >= 1; --i) {
    int c = idx[i];
    auto it_d = mset.lower_bound(c);
    ll d = *it_d;
    ll e = *(++it_d);
    ll b = *(-- --it_d);
    ll a = *(--it_d);

    ans += ((e - d) * (c - b) + (b - a) * (d - c)) * (ll)i;

    mset.insert(c);
  }

  cout << ans << endl;
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> da(n), li(n);
  rip(i, n, 0) {
    cin >> da[i];
    li[da[i] - 1] = i;
  }
  multiset<int> se;
  se.insert(-1);
  se.insert(-1);
  se.insert(n);
  se.insert(n);
  long long ans = 0;
  rip(i, n, 0) {
    int no = n - 1 - i;
    auto itr = lower_bound(se.begin(), se.end(), li[no]);
    int w = *++itr;
    itr--;
    int z = *itr;
    int y = *--itr;
    int x = *--itr;
    // printf("%d %d %d %d\n",x,y,z,w);
    ans += (ll)(no + 1) * ((y - x) * (z - li[no]) + (w - z) * (li[no] - y));
    se.insert(li[no]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> da(n), li(n);
  rip(i, n, 0) {
    cin >> da[i];
    li[da[i] - 1] = i;
  }
  multiset<int> se;
  se.insert(-1);
  se.insert(-1);
  se.insert(n);
  se.insert(n);
  long long ans = 0;
  rip(i, n, 0) {
    int no = n - 1 - i;
    auto itr = se.lower_bound(li[no]);
    int w = *++itr;
    itr--;
    int z = *itr;
    int y = *--itr;
    int x = *--itr;
    // printf("%d %d %d %d\n",x,y,z,w);
    ans += (ll)(no + 1) * ((y - x) * (z - li[no]) + (w - z) * (li[no] - y));
    se.insert(li[no]);
  }
  cout << ans << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 2100000000
#define MOD 1000000007

int main() {
  ll N;
  cin >> N;
  vector<pair<ll, ll>> P(N);
  for (ll i = 0; i < N; i++) {
    cin >> P.at(i).first;
    P.at(i).second = i;
  }
  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());

  multiset<ll> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    auto iter = upper_bound(S.begin(), S.end(), P.at(i).second);
    iter--;
    iter--;
    ll a1 = *iter;
    iter++;
    ll a2 = *iter;
    iter++;
    ll a3 = *iter;
    iter++;
    ll a4 = *iter;
    ans +=
        (ll)P.at(i).first * (((ll)a2 - (ll)a1) * ((ll)a3 - (ll)P.at(i).second) +
                             ((ll)a4 - (ll)a3) * ((ll)P.at(i).second - (ll)a2));
    S.insert(P.at(i).second);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 2100000000
#define MOD 1000000007

int main() {
  ll N;
  cin >> N;
  vector<pair<ll, ll>> P(N);
  for (ll i = 0; i < N; i++) {
    cin >> P.at(i).first;
    P.at(i).second = i;
  }
  sort(P.begin(), P.end());
  reverse(P.begin(), P.end());

  multiset<ll> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    auto iter = S.upper_bound(P.at(i).second);
    iter--;
    iter--;
    ll a1 = *iter;
    iter++;
    ll a2 = *iter;
    iter++;
    ll a3 = *iter;
    iter++;
    ll a4 = *iter;
    ans +=
        (ll)P.at(i).first * (((ll)a2 - (ll)a1) * ((ll)a3 - (ll)P.at(i).second) +
                             ((ll)a4 - (ll)a3) * ((ll)P.at(i).second - (ll)a2));
    S.insert(P.at(i).second);
  }
  cout << ans << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

int main() {
  int N;
  cin >> N;
  int P[N];
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  int pos[N];
  rep(i, N) pos[(N - 1) - P[i]] = i;
  multiset<int> gr;
  gr.insert(-1);
  gr.insert(-1);
  gr.insert(N);
  gr.insert(N);
  ll result = 0;
  rep(i, N) {
    auto r1 = upper_bound(gr.begin(), gr.end(), pos[i]);
    auto r2 = r1;
    r2++;
    auto l1 = r1;
    l1--;
    auto l2 = l1;
    l2--;
    ll tmp = (pos[i] - *l1) * (*r2 - *r1) + (*r1 - pos[i]) * (*l1 - *l2);
    result += tmp * (N - i);
    // cout << *l2 << " " << *l1 << " " << *r1 << " " << *r2 << endl;
    // cout << N-i << ":" << tmp << endl;
    gr.insert(pos[i]);
  }
  cout << result << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

int main() {
  int N;
  cin >> N;
  int P[N];
  rep(i, N) {
    cin >> P[i];
    P[i]--;
  }
  int pos[N];
  rep(i, N) pos[(N - 1) - P[i]] = i;
  multiset<int> gr;
  gr.insert(-1);
  gr.insert(-1);
  gr.insert(N);
  gr.insert(N);
  ll result = 0;
  rep(i, N) {
    auto r1 = gr.upper_bound(pos[i]);
    auto r2 = r1;
    r2++;
    auto l1 = r1;
    l1--;
    auto l2 = l1;
    l2--;
    ll tmp = (pos[i] - *l1) * (*r2 - *r1) + (*r1 - pos[i]) * (*l1 - *l2);
    result += tmp * (N - i);
    // cout << *l2 << " " << *l1 << " " << *r1 << " " << *r2 << endl;
    // cout << N-i << ":" << tmp << endl;
    gr.insert(pos[i]);
  }
  cout << result << endl;
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  map<ll, ll> p;
  rep(i, n) {
    ll temp;
    cin >> temp;
    p.ins(mp(temp, i));
  }

  multiset<ll> q;
  q.ins(-1);
  q.ins(-1);
  q.ins(n);
  q.ins(n);

  ll ans = 0;
  for (int i = n; i >= 1; i--) {
    auto bs = lower_bound(q.begin(), q.end(), p[i]);
    ll r1 = *bs;
    bs++;
    ll r2 = *bs;
    bs--;
    bs--;
    ll l2 = *bs;
    bs--;
    ll l1 = *bs;

    ans += i * (l2 - l1) * (r1 - p[i]);
    ans += i * (p[i] - l2) * (r2 - r1);
    q.ins(p[i]);
  }

  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  map<ll, ll> p;
  rep(i, n) {
    ll temp;
    cin >> temp;
    p.ins(mp(temp, i));
  }

  multiset<ll> q;
  q.ins(-1);
  q.ins(-1);
  q.ins(n);
  q.ins(n);

  ll ans = 0;
  for (int i = n; i >= 1; i--) {
    auto bs = q.lower_bound(p[i]);
    ll r1 = *bs;
    bs++;
    ll r2 = *bs;
    bs--;
    bs--;
    ll l2 = *bs;
    bs--;
    ll l1 = *bs;

    ans += i * (l2 - l1) * (r1 - p[i]);
    ans += i * (p[i] - l2) * (r2 - r1);
    q.ins(p[i]);
  }

  cout << ans << "\n";
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  const int MXZ = 1024;
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  ll ans = 0;
  cin >> n;
  vector<pii> p(n);
  rep(i, n) {
    cin >> p[i].first;
    p[i].second = i + 1;
  }
  sort(p.rbegin(), p.rend());
  bitset<MXZ> bs1, bs2;

  bs1[0] = bs2[0] = 1;
  bs1[n + 1] = bs2[n + 1] = 1;
  rep(i, n) {
    int cid = p[i].second;
    bs1[cid] = bs2[n - cid] = 1;
    int l1 = 0, l2 = 0;
    int r1 = n + 1, r2 = n + 1;

    auto it = bs1._Find_next(cid);
    if (it <= n) {
      r1 = it;
      it = bs1._Find_next(r1);
      if (it <= n)
        r2 = it;
    }
    it = bs2._Find_next(n - cid);
    if (it <= n) {
      l2 = it;
      it = bs2._Find_next(l2);
      if (it <= n)
        l1 = n - it;
      l2 = n - l2;
    }

    ll a = cid - l2, b = l2 - l1;
    ll c = r1 - cid, d = r2 - r1;
    ans += (a * d + b * c) * p[i].first;
    // dbg(i, p[i].first, l1,l2,r1,r2, a*d+b*c, ans);
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  const int MXZ = 1 << 17;
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  ll ans = 0;
  cin >> n;
  vector<pii> p(n);
  rep(i, n) {
    cin >> p[i].first;
    p[i].second = i + 1;
  }
  sort(p.rbegin(), p.rend());
  bitset<MXZ> bs1, bs2;

  bs1[0] = bs2[0] = 1;
  bs1[n + 1] = bs2[n + 1] = 1;
  rep(i, n) {
    int cid = p[i].second;
    bs1[cid] = bs2[n - cid] = 1;
    int l1 = 0, l2 = 0;
    int r1 = n + 1, r2 = n + 1;

    auto it = bs1._Find_next(cid);
    if (it <= n) {
      r1 = it;
      it = bs1._Find_next(r1);
      if (it <= n)
        r2 = it;
    }
    it = bs2._Find_next(n - cid);
    if (it <= n) {
      l2 = it;
      it = bs2._Find_next(l2);
      if (it <= n)
        l1 = n - it;
      l2 = n - l2;
    }

    ll a = cid - l2, b = l2 - l1;
    ll c = r1 - cid, d = r2 - r1;
    ans += (a * d + b * c) * p[i].first;
    // dbg(i, p[i].first, l1,l2,r1,r2, a*d+b*c, ans);
  }
  cout << ans << "\n";
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
/* while(!success_achieved())
      work_hard();

   code by:aman_ayush
  */
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
  ll n;
  cin >> n;
  ll arr[n], pos[n + 1];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    pos[arr[i]] = i;
  }
  multiset<ll> ms;
  ms.insert(-1);
  ms.insert(-1);
  ms.insert(n);
  ms.insert(n);
  ms.insert(pos[n]);
  ll cnt = 0;
  for (ll i = n - 1; i >= 1; i--) {
    auto itr = upper_bound(ms.begin(), ms.end(), pos[i]);
    ll r1 = *itr;
    itr++;
    ll r2 = *itr;
    for (int j = 0; j < 2; j++)
      itr--;
    ll l1 = *itr;
    itr--;
    ll l2 = *itr;
    cnt += i * ((r2 - r1) * (pos[i] - l1) + (l1 - l2) * (r1 - pos[i]));
    ms.insert(pos[i]);
  }
  cout << cnt << "\n";
  return 0;
}


Fixed submission:
/* while(!success_achieved())
      work_hard();

   code by:aman_ayush
  */
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
  ll n;
  cin >> n;
  ll arr[n], pos[n + 1];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    pos[arr[i]] = i;
  }
  multiset<ll> ms;
  ms.insert(-1);
  ms.insert(-1);
  ms.insert(n);
  ms.insert(n);
  ms.insert(pos[n]);
  ll cnt = 0;
  for (ll i = n - 1; i >= 1; i--) {
    auto itr = ms.upper_bound(pos[i]);
    ll r1 = *itr;
    itr++;
    ll r2 = *itr;
    for (int j = 0; j < 2; j++)
      itr--;
    ll l1 = *itr;
    itr--;
    ll l2 = *itr;
    cnt += i * ((r2 - r1) * (pos[i] - l1) + (l1 - l2) * (r1 - pos[i]));
    ms.insert(pos[i]);
  }
  cout << cnt << "\n";
  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void print(const vector<int> &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  VI p(n), pos(n);
  multiset<int> s;
  FOR(i, 1, n + 1) {
    cin >> p[i];
    pos[p[i]] = i;
  }
  s.insert(0);
  s.insert(0);
  s.insert(n + 1);
  s.insert(n + 1);

  ll ans = 0;
  for (int i = n; i >= 1; --i) {
    auto it = s.lower_bound(pos[i]);
    int a = *(++it);
    int b = *(--it);
    int c = *(--it);
    int d = *(--it);
    ans += 1LL * ((a - b) * (pos[i] - c) + (c - d) * (b - pos[i])) * i;
    s.insert(pos[i]);
  }

  out(ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void print(const vector<int> &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  VI p(n + 3), pos(n + 3);
  multiset<int> s;
  FOR(i, 1, n + 1) {
    cin >> p[i];
    pos[p[i]] = i;
  }
  s.insert(0);
  s.insert(0);
  s.insert(n + 1);
  s.insert(n + 1);

  ll ans = 0;
  for (int i = n; i >= 1; --i) {
    auto it = s.lower_bound(pos[i]);
    int a = *(++it);
    int b = *(--it);
    int c = *(--it);
    int d = *(--it);
    ans += 1LL * ((a - b) * (pos[i] - c) + (c - d) * (b - pos[i])) * i;
    s.insert(pos[i]);
  }

  out(ans);

  return 0;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> o(n);
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    o.at(p - 1) = i;
  }
  set<int> s;
  s.insert(0);
  s.insert(n + 1);
  int64_t a = 0;
  for (int i = n - 1; i >= 0; i--) {
    auto l = prev(lower_bound(s.begin(), s.end(), o.at(i)));
    auto r = lower_bound(s.begin(), s.end(), o.at(i));
    int64_t xl = (*l == 0 ? 0 : (int64_t)(*l - *prev(l)) * (*r - o.at(i)));
    int64_t xr = (*r == n + 1 ? 0 : (int64_t)(o.at(i) - *l) * (*next(r) - *r));
    a += (xl + xr) * (i + 1);
    s.insert(o.at(i));
  }
  cout << a << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> o(n);
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    o.at(p - 1) = i;
  }
  set<int> s;
  s.insert(0);
  s.insert(n + 1);
  int64_t a = 0;
  for (int i = n - 1; i >= 0; i--) {
    auto l = prev(s.lower_bound(o.at(i)));
    auto r = s.lower_bound(o.at(i));
    int64_t xl = (*l == 0 ? 0 : (int64_t)(*l - *prev(l)) * (*r - o.at(i)));
    int64_t xr = (*r == n + 1 ? 0 : (int64_t)(o.at(i) - *l) * (*next(r) - *r));
    a += (xl + xr) * (i + 1);
    s.insert(o.at(i));
  }
  cout << a << endl;
}
Problem p02919 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define exists(s, x) (s).find(x) != (s).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define fi first
#define se second
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

inline ll add(ll x, ll y) { return (x + y) % MOD; }
inline ll mul(ll x, ll y) { return (x * y) % MOD; }
inline ll sub(ll x, ll y) {
  ll res = x - y;
  if (res < 0)
    res += MOD;
  return res;
}
inline ll modpow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  vector<int> IP(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
    IP[P[i]] = i;
  }
  multiset<int> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    auto it = lower_bound(all(S), IP[i]);
    int r1 = *it;
    it++;
    int r2 = *it;
    it--;
    it--;
    int l1 = *it;
    it--;
    int l2 = *it;
    // cout << l2 << " " << l1 << " " << IP[i] << " " << r1 << " " << r2 <<
    // endl;
    ll tmp = 0;
    tmp += (ll)(r1 - IP[i]) * (l1 - l2);
    tmp += (ll)(r2 - r1) * (IP[i] - l1);
    ans += tmp * (i + 1);
    S.insert(IP[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define exists(s, x) (s).find(x) != (s).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define fi first
#define se second
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

inline ll add(ll x, ll y) { return (x + y) % MOD; }
inline ll mul(ll x, ll y) { return (x * y) % MOD; }
inline ll sub(ll x, ll y) {
  ll res = x - y;
  if (res < 0)
    res += MOD;
  return res;
}
inline ll modpow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  vector<int> IP(N);
  REP(i, N) {
    cin >> P[i];
    P[i]--;
    IP[P[i]] = i;
  }
  multiset<int> S;
  S.insert(-1);
  S.insert(-1);
  S.insert(N);
  S.insert(N);
  ll ans = 0;
  for (int i = N - 1; i >= 0; --i) {
    auto it = S.lower_bound(IP[i]);
    int r1 = *it;
    it++;
    int r2 = *it;
    it--;
    it--;
    int l1 = *it;
    it--;
    int l2 = *it;
    // cout << l2 << " " << l1 << " " << IP[i] << " " << r1 << " " << r2 <<
    // endl;
    ll tmp = 0;
    tmp += (ll)(r1 - IP[i]) * (l1 - l2);
    tmp += (ll)(r2 - r1) * (IP[i] - l1);
    ans += tmp * (i + 1);
    S.insert(IP[i]);
  }
  cout << ans << endl;
  return 0;
}