Submission #2873076


Source Code Expand

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

int N;
int a[200010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  int pos[200010];
  for (int i = 0; i < N; i++) {
    pos[a[i] - 1] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(N);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int L = pos[i] - *(--st.lower_bound(pos[i]));
    int R = *(st.upper_bound(pos[i])) - pos[i];
    ans += (ll)L * R * (i + 1);
    st.insert(pos[i]);
  }
  cout << ans << endl;

  return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User legosuke
Language C++14 (GCC 5.4.1)
Score 400
Code Size 688 Byte
Status AC
Exec Time 161 ms
Memory 11264 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample example0, example1, example2
All corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name Status Exec Time Memory
corner0 AC 160 ms 11264 KB
corner1 AC 161 ms 11264 KB
corner2 AC 1 ms 256 KB
corner3 AC 86 ms 11264 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 126 ms 11264 KB
maxrand1 AC 126 ms 11264 KB
maxrand2 AC 126 ms 11136 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB