#include <bits/stdc++.h>using namespace std;
const long long N = 5e5 + 5, M = 55;
long long n, k, ans, lg[N], dp[N][M], sum[N], pos[N][M];
struct S {
long long sx, l, r, x, o;
friend bool operator < (const S &X, const S &Y) {
return X.x < Y.x;
}
};
priority_queue<S> pq;
void D() {
for (long long j = 1; j <= lg[n]; j++) {
for (long long i = 1; i + (1 << j) - 1 <= n; i++) {
if (dp[i][j - 1] > dp[i + (1 << (j - 1))][j - 1]) {
dp[i][j] = dp[i][j - 1];
pos[i][j] = pos[i][j - 1];
} else {
dp[i][j] = dp[i + (1 << (j - 1))][j - 1];
pos[i][j] = pos[i + (1 << (j - 1))][j - 1];
}
}
}
}
void E(long long sx, long long l, long long r) {
long long tmp = lg[r - l + 1], o;
if (dp[l][tmp] > dp[r - (1 << tmp) + 1][tmp]) {
o = pos[l][tmp];
} else {
o = pos[r - (1 << tmp) + 1][tmp];
}
pq.push({sx, l, r, sum[o] - sum[sx - 1], o});
}
int main() {
// freopen("T2.in", "r", stdin);// freopen("T2.out", "w", stdout);long long l, r;
cin >> n >> k >> l >> r;
lg[0] = -1;
for (long long i = 1; i <= n; i++) {
cin >> dp[i][0];
pos[i][0] = i;
lg[i] = lg[i / 2] + 1;
dp[i][0] = sum[i] = sum[i - 1] + dp[i][0];
}
D();
for (long long i = 1; i + l - 1 <= n; i++) {
E(i, i + l - 1, min(n, i + r - 1));
}
for (long long i = 1; i <= k; i++) {
S cur = pq.top();
pq.pop();
ans += cur.x;
if (cur.o > cur.l) {
E(cur.sx, cur.l, cur.o - 1);
}
if (cur.o < cur.r) {
E(cur.sx, cur.o + 1, cur.r);
}
}
cout << ans;
return 0;
}
abc325f
C++
#include <bits/stdc++.h>using namespace std;
const long long N = 2e5 + 5, M = 105, K = 1005;
long long a[N], f[M][K], p[2], o[2], k[2], n, ans = 1e18;
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 0; i < 2; i++) {
cin >> p[i] >> o[i] >> k[i];
}
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j <= k[0]; j++) {
for (long long e = 0; e <= j; e++) {
f[i][j] = min(f[i][j], f[i - 1][j - e] + max((long long)0, (a[i] - e * p[0] + p[1] - 1) / p[1]));
}
}
}
for (long long i = 0; i <= k[0]; i++) {
if (f[n][i] <= k[1]) {
ans = min(ans, (i * o[0]) + (f[n][i] * o[1]));
}
}
cout << ((ans == 1e18) ? -1 : ans);
return 0;
}
abc391f
C++
#include <bits/stdc++.h>using namespace std;
const long long N = 2e5 + 5;
long long a[N], b[N], c[N], n, k, r;
bool cmp(long long x, long long y) {
return x > y;
}
long long E(long long x, long long y, long long o) {
long long l = 1, r = n;
while (l <= r) {
long long mid = (l + r) / 2;
long long cnt = (x * c[mid]) + y;
if (cnt < o) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return r;
}
long long Check(long long x) {
long long cnt = k;
for (long long i = 1; i <= n && cnt >= 0; i++) {
for (long long j = 1; j <= n && cnt >= 0; j++) {
long long tmp = cnt;
long long o = E(a[i] + b[j], a[i] * b[j], x);
if (!o) {
break;
}
cnt -= o;
}
}
return cnt;
}
long long D() {
long long l = 0;
while (l <= r) {
long long mid = (l + r) / 2;
if (Check(mid) > 0) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
for (long long i = 1; i <= n; i++) {
cin >> b[i];
}
for (long long i = 1; i <= n; i++) {
cin >> c[i];
}
sort (a + 1, a + 1 + n, cmp);
sort (b + 1, b + 1 + n, cmp);
sort (c + 1, c + 1 + n, cmp);
r = (a[1] * b[1]) + (a[1] * c[1]) + (b[1] * c[1]);
cout << D();
return 0;
}
atdpc
C++
#include <bits/stdc++.h>using namespace std;
const int N = 1e5 + 5;
int a[N], b[N], c[N], f[N], dp[N], s[N], n;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i] >> c[i];
}
for (int i = 1; i <= n; i++) {
f[i] = max (dp[i - 1], s[i - 1]) + a[i];
dp[i] = max (f[i - 1], s[i - 1]) + b[i];
s[i] = max (dp[i - 1], f[i - 1]) + c[i];
}
cout << max(f[n], max(dp[n], s[n]));
return 0;
}
arc084a
C++
#include <bits/stdc++.h>using namespace std;
const int N = 1e5 + 5;
long long a[N], b[N], c[N], n, sum, g, e, j;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
sort (a + 1, a + 1 + n);
sort (c + 1, c + 1 + n);
for (int i = 1; i <= n; i++) {
j = lower_bound(a + 1, a + 1 + n, b[i]) - a - 1;
e = upper_bound(c + 1, c + 1 + n, b[i]) - c;
g = n - e + 1;
if (g && j) {
sum += (g * j);
}
}
cout << sum;
return 0;
}
CF1791C
C++
#include <bits/stdc++.h>using namespace std;
const int N = 2e3 + 5;
int n, t, ans;
string a;
int main() {
for (cin >> t; t--; ) {
cin >> n >> a;
a = " " + a;
for (int i = 1, r = n; i <= r; i++, r--) {
if (a[i] == a[r]) {
ans = r - i + 1;
break;
}
}
cout << ans << "\n";
ans = 0;
}
return 0;
}