牛客算法周周练15 B 可持久化动态图上树状数组维护01背包
可持久化动态图上树状数组维护01背包
https://ac.nowcoder.com/acm/contest/6290/B
B 可持久化动态图上树状数组维护01背包
题目地址:
基本思路:
名字非常的唬人,然而是一个***题,
我们先将负数从后往前删,就能保证它们在自己所在的位置被删掉,获得最小的负值,
然后再让正数都在第一个位置删掉,获得最小正值就可以了。
参考代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define IO std::ios::sync_with_stdio(false) #define int long long #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, _) memset(s, _, sizeof(s)) #define pb push_back #define pii pair <int, int> #define mp(a, b) make_pair(a, b) #define INF (int)1e18 inline int read() { int x = 0, neg = 1; char op = getchar(); while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); } while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); } return neg * x; } inline void print(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } const int maxn = 1e6 + 10; int n,a[maxn]; signed main() { IO; cin >> n; int sum = 0; rep(i,1,n){ cin >> a[i]; if(a[i] < 0) sum += a[i] * i; else sum += a[i]; } cout << sum << '\n'; return 0; }