This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
int n;
vector<pair<int, int>> a;
bool check(pair<int, int> A, pair<int, int> B) {
// cout << A.ff << " " << A.ss << " " << B.ff << " " << B.ss << endl;
return 1ll * A.ff * (A.ss + B.ss) >= 1ll * B.ff * A.ss;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int val; cin >> val;
if (a.empty() || a.back().ff < val) a.emplace_back(val, 1);
else ++a.back().ss;
}
vector<pair<int, int>> na;
na.push_back(a[0]);
for (int i = 1; i < isz(a); ++i) {
if (check(na.back(), a[i])) {
na.back().ff = a[i].ff;
na.back().ss += a[i].ss;
}
else {
na.push_back(a[i]);
}
}
i64 res = 0, cur = 0;
for (auto [val, len] : na) {
cur += val;
res += 1ll * cur * len;
}
cout << res << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
cout << "Check array size pls sir" << endl;
#endif
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |