text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; long long int ncr[1001][1001]; void ncrdp(void) { for (int i = 0; i < 1001; i++) { ncr[i][0] = 1; ncr[i][i] = 1; } for (int i = 1; i < 1001; i++) { for (int j = 1; j < i; j++) { ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % 1000000007; } } } long long fast_power(long long base, long long power) { long long result = 1; while (power > 0) { if (power % 2 == 1) { result = (result * base) % 1000000007; } base = (base * base) % 1000000007; power = power / 2; } return result; } bool prime[5000000]; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int operation(long long int var1, long long int var2) { return min(var1, var2); } void build(long long int n, long long int l, long long int r, long long int tree[], long long int a[]) { if (l == r) { tree[n] = a[l]; } else { build(2 * n, l, (l + r) / 2, tree, a); build(2 * n + 1, (l + r) / 2 + 1, r, tree, a); tree[n] = operation(tree[2 * n], tree[2 * n + 1]); } } void update(long long int n, long long int l, long long int r, long long int tree[], long long int a[], long long int node, long long int update_value) { if (node >= l && node <= r) { if (l == r) { if (l == node) { tree[n] = update_value; } } else { update(2 * n, l, (l + r) / 2, tree, a, node, update_value); update(2 * n + 1, (l + r) / 2 + 1, r, tree, a, node, update_value); tree[n] = operation(tree[2 * n], tree[2 * n + 1]); } } } void query(long long int n, long long int l, long long int r, long long int tree[], long long int a[], long long int ql, long long int qr, long long int &min_value) { if (l >= ql && r <= qr) { min_value = operation(min_value, tree[n]); } else if ((l <= ql && ql <= r) || (l <= qr && qr <= r)) { query(2 * n, l, (l + r) / 2, tree, a, max(ql, l), min(qr, r), min_value); query(2 * n + 1, (l + r) / 2 + 1, r, tree, a, max(ql, l), min(qr, r), min_value); } } long long int tree[1000000]; int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0; *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } int get_prime(int x) { while (x > 1) { if (prime[x]) { return x; } x--; } return 1; } bool findpath(char g[100][100], int i, int j, int n) { if (i == n - 1 || j == n - 1) { return true; } if (g[i + 1][j] == '1' || g[i][j + 1] == '1') { if (g[i + 1][j] == '1') return findpath(g, i + 1, j, n); if (g[i][j + 1] == '1') return findpath(g, i, j + 1, n); } return false; } long long int toNum(string s) { long long int n = 0; long long int l = s.length(); long long int p2 = 1; while (l--) { if (s[l] == '1') { n += p2; } p2 *= 2; } return n; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < (n); i++) { cin >> v[i]; } for (int i = 0; i < (100); i++) { for (int i = 0; i < (n); i++) { for (int j = (i + 1); j < (n); j++) { if (i - v[i] == j - v[j]) { swap(v[i], v[j]); } } } } for (int i = 0; i < (n); i++) { cout << v[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); for (int i = n - 1; i >= 0; i--) cout << ar[i] << " "; cout << "\n"; } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200; int t; int v[MAXN]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); cout.tie(0); cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; sort(v, v + n); reverse(v, v + n); for (int i = 0; i < n; i++) cout << v[i] << " "; cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int t, n, a[100]; int main() { cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; i--) cout << a[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; long long max(long long i, long long j) { return i > j ? i : j; } long long min(long long i, long long j) { return i < j ? i : j; } int hcf(int a, int b) { if (b == 0) return a; return hcf(b, a % b); } int ncr(int n, int r) { if (r == 0) return 1; if (n == r) return 1; if (r == 1) return n; return ncr(n - 1, r) + ncr(n - 1, r - 1); } bool isPrime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } bool sortValD(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } vector<int> sieve(int n) { vector<bool> prime(n + 1, true); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } vector<int> v; for (int i = 2; i < prime.size() + 2; i++) { if (prime[i]) v.push_back(i); } return v; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } sort(v.begin(), v.end(), greater<int>()); for (int i = 0; i < n; i++) { cout << v[i] << " "; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long N = 100000; const long long __P = 8; template <typename first, typename second> ostream &operator<<(ostream &os, const pair<first, second> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename first, typename second> ostream &operator<<(ostream &os, const map<first, second> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } int32_t main() { long long u1, u2, u3; u3 = 0 + 1; ios_base::sync_with_stdio(false); cin.tie(0); u3 = 0 + 1; u3 = 0 + 1; long long t; cin >> t; u3 = 0 + 1; while (t--) { u3 = 0 + 1; long long n; cin >> n; u3 = 0 + 1; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } u3 = 0 + 1; sort(a.begin(), a.end(), greater<long long>()); u3 = 0 + 1; for (auto t : a) cout << t << " "; cout << '\n'; u3 = 0 + 1; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[100000]; bool com(int x, int y) { return x > y; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, com); for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long res = 1; a = a % 1000000007; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b /= 2; } return res; } long long invmod(long long a) { return power(a, 1000000007 - 2); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (auto &i : arr) cin >> i; sort(arr, arr + n, greater<int>()); for (auto i : arr) cout << i << ' '; cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int T; int n; int a[120]; void solve() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> T; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char ch = getchar(); int f = 0; x = 0; while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); if (f) x = -x; } int a[123]; signed main() { int T; read(T); while (T--) { int n; read(n); for (int i = 1; i <= n; ++i) read(a[i]); sort(a + 1, a + n + 1); for (int i = n; i >= 1; --i) printf("%d ", a[i]); puts(""); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; const int MOD = 1e9 + 7; const int inf = 1e9 + 7; const double PI = acos(-1); const double e = 2.718281828459; int a[105]; int main() { int T, n, m; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1, greater<int>()); for (int i = 1; i <= n; i++) { printf("%d ", a[i]); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], x; for (x = 0; x < n; x++) cin >> a[x]; sort(a, a + n); for (x = n - 1; x >= 0; x--) cout << a[x] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int n; int a[105]; bool cmp(int x, int y) { return x > y; } int main() { int t; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int A[n]; for (long long int i = 0; i < n; i++) cin >> A[i]; sort(A, A + n); for (int i = n - 1; i >= 0; i--) cout << A[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = n - 1; i >= 0; i--) cout << arr[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T_vector> void output_vector(const T_vector& v, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n'); } void run_sample() { int N; cin >> N; vector<int> A(N); for (auto& d : A) cin >> d; sort(A.begin(), A.end()); reverse(A.begin(), A.end()); output_vector(A); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; cin >> t; while (t--) run_sample(); }
#include <bits/stdc++.h> using namespace std; bool isGood(vector<long long> &a) { for (long long i = 0; i < a.size(); i++) { for (long long j = i + 1; j < a.size(); j++) { if ((i - a[i]) == (j - a[j])) { return false; } } } return true; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long test; cin >> test; while (test--) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); if (isGood(a)) { for (auto t : a) cout << t << " "; cout << '\n'; } else { sort(a.begin(), a.end(), greater<long long>()); if (isGood(a)) { for (auto t : a) cout << t << " "; cout << '\n'; } else { assert(0); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<long long int>()); for (long long int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n, a[200]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = n - 1; i > 0; i--) printf("%d ", a[i]); printf("%d\n", a[0]); } return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(int a, int b) { return a > b; } int main() { int testcase, len, x; vector<int> v; cin >> testcase; while (testcase--) { cin >> len; for (int i = 0; i < len; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end(), cmp); cout << v[0]; for (int i = 1; i < len; i++) { cout << " " << v[i]; } if (testcase != 0) printf("\n"); v.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int loop; cin >> loop; for (int lo = 0; lo < loop; lo++) { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr.begin(), arr.end()); for (int i = n - 1; i >= 0; i--) cout << arr[i] << " "; cout << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int test_case; cin >> test_case; while (test_case--) { int n; cin >> n; vector<int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; sort(vec.rbegin(), vec.rend()); for (int i = 0; i < n; i++) cout << vec[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int arr[N]; int main() { int t; cin >> t; while (t--) { int a; cin >> a; for (int i = 1; i <= a; i++) { cin >> arr[i]; } sort(arr + 1, arr + a + 1); reverse(arr + 1, arr + a + 1); for (int i = 1; i <= a; i++) { cout << arr[i] << " "; } cout << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("unroll-loops") using namespace std; inline bool kthbit(long long n, int k) { return (n >> k) & 1; } inline long long setkthbit(long long n, int k) { return n | (1ll << k); } inline long long unsetkthbit(long long n, int k) { return n & ~(1ll << k); } inline long long flipkthbit(long long n, int k) { return n ^ (1ll << k); } long long nc2(long long n) { return (1ll * n * (n - 1)) / 2; } long long nsum(long long n) { return (1ll * n * (n + 1)) / 2; } void Solve() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; ++i) cin >> a[i]; sort(a.begin(), a.end()); reverse(a.begin(), a.end()); for (long long i = 0; i < n; ++i) cout << a[i] << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int tests = 1; cin >> tests; while (tests--) { Solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, i, j, x; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); reverse(a, a + n); for (i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long int mod = 1000000007; long long int fact[1000001]; void fac() { fact[0] = 1; for (int i = 1; i <= 1000000; i++) fact[i] = fact[i - 1] * i; } void BOOST() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long int power(long long int x, long long int y) { long long int res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long int modInverse(long long int n) { return power(n, mod - 2); } long long int min(long long int a, long long int b) { if (b <= a) return b; return a; } long long int max(long long int a, long long int b) { if (a >= b) return a; return b; } long long int ncr(long long int n, long long int r) { if (r == 0) return 1; return ((((fact[n] % mod) * (modInverse(fact[r]) % mod)) % mod) * (modInverse(fact[n - r]) % mod)) % mod; } int main() { BOOST(); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.rbegin(), v.rend()); for (auto &x : v) cout << x << " "; cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; bool cmp(int& a, int& b) { return a > b; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end(), cmp); for (int i = 0; i < n; i++) cout << v[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void testcase() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a.begin(), a.end(), greater<int>()); for (auto x : a) { printf("%d ", x); } printf("\n"); } int main() { int T; scanf("%d", &T); for (int t = 1; t <= T; t++) { testcase(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a[101], i, j; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); while (i--) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e7 + 10; int T, n, m, k, q, x, y, z; int a[maxn], cnt[maxn]; char s[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { if (i != 1) cout << ' '; cout << a[n - i + 1]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; char nl = '\n'; using Int = long long; long long re[100]; void solve() { long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> re[i]; } sort(re, re + n); for (long long i = n - 1; i >= 0; i--) { cout << re[i] << " "; } cout << nl; } signed main() { long long t = 1; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], i, t = 0; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); reverse(a, a + n); for (i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); for (int i = n - 1; i >= 0; --i) cout << v[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int q; void s() { int n, a[101010]; int b[101010]; vector<int> z, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); for (int i = n; i >= 1; i--) { cout << a[i] << " "; } cout << endl; } int main() { cin >> q; while (q) { s(); q--; } return 0; }
#include <bits/stdc++.h> const int maxn = 1e2 + 5; using namespace std; int m, n; int a[maxn], vis[maxn]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1); for (int i = n; i >= 1; i--) { printf("%d ", a[i]); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; bool comp(int x1, int x2) { return (x1 > x2); } int main() { int t; cin >> t; vector<int> vec[101]; int k = 0; while (t) { int v[101], n; cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; sort(v + 1, v + n + 1, comp); ++k; for (int i = 1; i <= n; i++) vec[k].push_back(v[i]); t--; } for (int i = 1; i <= k; i++, cout << '\n') for (int j = 0; j < vec[i].size(); j++) cout << vec[i][j] << " "; }
#include <bits/stdc++.h> using namespace std; int fun(const vector<int> &v, int n) { for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (j - v[j] == i - v[i]) { void(0); ; return -1; } } } return 1; } void solve() { int n; cin >> n; vector<int> a(n); for (auto &x : a) { cin >> x; } sort(a.rbegin(), a.rend()); for (auto &x : a) { cout << x << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; while (n--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; i--) cout << a[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); reverse(arr, arr + n); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; char a[1000][1010]; int solve() { int n, m; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end(), greater<int>()); for (int i = 0; i < n; ++i) { cout << a[i] << " "; } cout << "\n"; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n = 1; cin >> n; while (n--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; int a[n]; for (int i = 0; i <= n - 1; i++) { cin >> a[i]; } sort(a, a + n); for (int i = n - 1; i >= 0; i--) { cout << a[i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); for (int i = n - 1; i >= 0; i--) { cout << arr[i] << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void kaam() { int n, ctr = 0, max = 0; cin >> n; int arr[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); reverse(arr, arr + n); for (long long i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (j + 1 - arr[j] == i + 1 - arr[i]) swap(arr[i], arr[j]); } } for (long long i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) kaam(); return 0; }
#include <bits/stdc++.h> using namespace std; int read() { int out = 0, sgn = 1; char jp = getchar(); while (jp != '-' && (jp < '0' || jp > '9')) jp = getchar(); if (jp == '-') sgn = -1, jp = getchar(); while (jp >= '0' && jp <= '9') out = out * 10 + jp - '0', jp = getchar(); return out * sgn; } const int N = 100 + 10; int n, a[N]; void solve() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); for (int i = 1; i <= n; ++i) printf("%d ", a[i]); puts(""); } int main() { int T = read(); while (T--) solve(); return 0; }
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; char all[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int issatai; cin >> issatai; while (issatai--) { int n; cin >> n; int a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort((a) + 1, (a) + n + 1); reverse((a) + 1, (a) + n + 1); for (int i = 1; i <= n; i++) { cout << a[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int arr[105]; int main() { int i, j, k, a, b, c, n, t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &arr[i]); sort(arr + 1, arr + n + 1); for (i = 1; i <= n; i++) printf("%d ", arr[n - i + 1]); printf("\n"); } }
#include <bits/stdc++.h> void print(std::vector<int> const &input) { for (int i = 0; i < input.size(); i++) { std::cout << input.at(i) << ' '; } } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.rbegin(), v.rend()); print(v); cout << endl; v.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { int T, n, i, j; cin >> T; while (T--) { cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = n - 1; i >= 0; --i) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> long long int binpow(long long int a, long long int b) { if (b == 0) return 1; long long int res = binpow(a, b / 2); if (b % 2) return res * res * a; else return res * res; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n, 0); for (int i = 0; i < v.size(); i++) cin >> v[i]; sort(v.begin(), v.end(), greater<int>()); for (int i = 0; i < n; i++) cout << v[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; set<char> st = {'a', 'e', 'i', 'o', 'u'}; long long mod = 1e9 + 7; const int M = 100006; int f[M], inv[M]; int expo(int base, int exponent, int mod) { int ans = 1; while (exponent != 0) { if (exponent & 1) ans = (1LL * ans * base) % mod; base = (1LL * base * base) % mod; exponent >>= 1; } return ans % mod; } void compute() { f[0] = 1; for (long long i = 1; i <= M - 1; i++) { f[i] = (1LL * i * f[i - 1]) % mod; } inv[M - 1] = expo(f[M - 1], mod - 2, mod); for (int i = M - 2; i >= 0; i--) { inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod; } } int C(int n, int r) { return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod; } long long cal(long long i, double val, long long q) { long long ans; double tmp = (double)(i)*val; tmp *= (double)q; long long t = (long long)tmp; if (t == tmp) ans = t; else ans = (t + 1); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tests = 1; cin >> tests; while (tests--) { long long n, m; cin >> n; vector<long long> a(n); for (m = 0; m < n; m++) cin >> a[m]; sort(a.begin(), a.end(), greater<long long>()); for (auto x : a) cout << x << " "; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n, greater<int>()); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); } long long N = 1000000007; long long lmax = LONG_LONG_MAX; long long lmin = LONG_LONG_MIN; long long factorial(long long i) { long long product = 1; for (long long j = i; j > 0; j--) { product *= j; } return product; } int main() { fastio(); long long t; cin >> t; for (long long q = 0; q < t; q++) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; }; sort(a, a + n, greater<long long>()); for (long long i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); reverse(arr, arr + n); for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; t--; } }
#include <bits/stdc++.h> using namespace std; long long pw(long long x, long long y) { if (y == 0) return 1; if (y == 1) return x; long long ans = pw(x, y / 2); ans = ans * ans; if (y % 2 == 1) ans = ans * x; return ans; } long long pwm(long long x, long long y) { if (y == 0) return 1; if (y == 1) return x; long long ans = pwm(x, y / 2) % 1000000007; ans = (ans * ans) % 1000000007; if (y % 2 == 1) ans = (ans * x) % 1000000007; return ans; } int check(long long n) { if (n < 0) return 0; else return 1; } long long add(long long a, long long b) { return a + b; } long long max(long long a, long long b) { if (a > b) return a; else return b; } long long min(long long a, long long b) { if (a > b) return b; else return a; } long long minv(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m, t = m; m = a % m, a = t, t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void countsmall(long long n) { long long i = n; for (++i; i <= n; i += i & -i) for (; i; i -= i & -i) { } } const long long N = 1e3 + 10; string rev(string tmp) { string ok = ""; long long sz = tmp.size(); for (long long i = sz - 1; i >= 0; i--) ok += tmp[i]; return ok; } map<long long, long long> f; long long fnd(long long x) { if (f[x] == 0) { return x; } return f[x] == x ? x : f[x] = fnd(f[x]); } void solve() { long long n; cin >> n; long long a[n + 10]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<long long>()); ; for (long long i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void reader() {} void IOFast() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void solve() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<long long>()); for (long long i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; } signed main() { reader(); IOFast(); long long t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int t, n; int a[110]; int mycmp(int c, int d) { return c > d; } int main() { scanf("%d", &t); for (int k = 1; k <= t; k++) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1, mycmp); for (int i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i; cin >> n; vector<int> a(n); for (i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); for (i = a.size() - 1; i > 0; i--) cout << a[i] << " "; cout << a[0] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long ar[n]; for (long long i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n, greater<long long>()); for (long long i = 0; i < n - 1; i++) { for (long long j = i + 1; j < n; j++) { if ((i + 1) - ar[i] == (j + 1) - ar[j]) swap(ar[i], ar[j]); } } for (long long i = 0; i < n; i++) { cout << ar[i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, q, i; cin >> t; while (t--) { cin >> n; long long int arr[n + 2], a[n + 2]; for (i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); for (i = 0; i < n; i++) { a[i] = arr[n - i - 1]; } for (i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, t, a, b, c, i, j; cin >> t; while (t--) { cin >> n; vector<long long int> v, v1; for (i = 0; i < n; i++) { cin >> m; v.push_back(m); } sort(v.rbegin(), v.rend()); for (i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t, n, ans; cin >> t; while (t--) { cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) cout << a[n - 1 - i] << " "; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.rbegin(), a.rend()); for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; --i) cout << a[i] << ' '; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (long long i = n - 1; i >= 0; i--) { cout << a[i] << " "; } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; long long int ans; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; cin >> t; while (t--) { int n; cin >> n; int a[n]; int i; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, greater<int>()); for (i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int t, n, a[10001]; int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + 1 + n, greater<int>()); for (int i = 1; i <= n; ++i) printf("%d ", a[i]); putchar(10); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long int t, n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; long long int a[n], i; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (i = n - 1; i >= 0; i--) cout << a[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int n; vector<int> a; bool cmp(int x, int y) { return x > y; } void solve() { a.clear(); cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; a.push_back(x); } sort(a.begin(), a.end(), cmp); for (int u : a) { cout << u << " "; } cout << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i = 0; i < t; ++i) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i; cin >> n; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); reverse(a, a + n); for (i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; bool comp(long long a, long long b) { return a > b; } int main() { long long test; cin >> test; while (test--) { vector<long long> v; long long n; cin >> n; for (long long i = 1; i <= n; i++) { long long x; cin >> x; v.push_back(x); } sort(v.begin(), v.end(), comp); for (auto xx : v) { cout << xx << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int a[n]; for (int j = 0; j < n; j++) { cin >> a[j]; } sort(a, a + n); for (int k = 0; k < n; k++) { cout << a[n - k - 1] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i, j; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (i = n - 1; i >= 0; i--) { cout << a[i] << " "; } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; int n, v[1000]; int main() { int t; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; ++i) { cin >> v[i]; } sort(v + 1, v + n + 1); for (int i = n; i; --i) { cout << v[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; void Bogo(int n) { cin >> n; int bogo[n]; for (int i = 0; i < n; i++) { cin >> bogo[i]; } for (int k = 0; k < n - 1; k++) { int minindex = k; for (int l = k + 1; l < n; l++) { if (bogo[minindex] > bogo[l]) minindex = l; } swap(bogo[minindex], bogo[k]); } for (int j = n - 1; j >= 0; j--) { cout << bogo[j] << " "; } } int main() { int t; cin >> t; while (t > 0) { int num; Bogo(num); cout << endl; t--; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = n - 1; i >= 0; i--) cout << a[i] << " "; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { cout << a[n - 1 - i]; if (i != n - 1) { cout << " "; } } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } template <class T> inline T gcd(T a, T b) { return __gcd(a, b); } template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } long long modpow(long long a, long long n, long long temp) { long long res = 1, y = a; while (n > 0) { if (n & 1) res = (res * y) % temp; y = (y * y) % temp; n /= 2; } return res % temp; } long long getbit(long long j, long long i) { return ((i >> (j - 1ll)) & 1ll); } long long ison(long long mask, long long pos) { return (mask & (1 << pos)); } long long cbit(long long n) { long long k = 0; while (n) n &= (n - 1), k++; return k; } long long nbit(long long n) { long long k = 0; while (n) n /= 2, k++; return k; } long long mod = 998244353; int sgn(long long x) { return x < 0 ? -1 : !!x; } const double eps = 6 * 1e-8; const double pi = acos(-1.0); inline void inc(long long &x, long long y) { x += y; if (x >= mod) x -= mod; } inline void dec(long long &x, long long y) { x -= y; if (x < 0) x += mod; } inline void chmax(long long &x, long long y) { if (y > x) x = y; } inline void mulm(long long &x, long long y) { x *= y; if (x >= mod) x -= mod; } long long xo(long long i) { if ((i & 3) == 0) return i; if ((i & 3) == 1) return 1; if ((i & 3) == 2) return i + 1; return 0; } long long a[1000050]; int main() { long long n, t; cin >> t; while (t--) { long long n, m; cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); for (long long i = n; i >= 1; i--) cout << a[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vl = vector<ll>; void divisors(map<int, int>& d, ll N) { for (int div : {2, 3, 5}) { int cnt = 1; if (N % 2 == 0) { while ((N % 2) == 0) { N /= 2; cnt++; } d[div] = cnt; } } static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (ll div = 7; div * div <= N; div += increments[i++]) { if ((N % div) == 0) { int cnt = 1; while ((N % i) == 0) { N /= i; cnt++; } d[i] = cnt; } if (i == 8) i = 0; } if (N > 1) d[N] = 2; } int main() { int T, N; cin >> T; while (T--) { cin >> N; vi v(N); for (int i = 0; i < N; i++) cin >> v[i]; sort(begin(v), end(v)); reverse(begin(v), end(v)); for (auto& x : v) { cout << x << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, a[500]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); cout << a[n - 1]; for (int i = 1; i < n; i++) cout << " " << a[n - 1 - i]; cout << endl; } }
#include <bits/stdc++.h> using namespace std; vector<long long> bitsrev(vector<long long> arr, long long x) { while (x > 0) { arr.push_back(x % 2); x /= 2; } return arr; } void equateLen(vector<long long> &el, vector<long long> &ar) { if (el.size() != ar.size()) { if (el.size() < ar.size()) { while (el.size() != ar.size()) { el.push_back(0); } } else { while (el.size() != ar.size()) { ar.push_back(0); } } } } long long search(vector<long long> arr, long long l, long long r, long long x) { if (r >= l) { long long mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return search(arr, l, mid - 1, x); return search(arr, mid + 1, r, x); } return -1; } long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long modInverse(long long n, long long p) { return power(n, p - 2, p); } long long CmP(long long n, long long r, long long p = 1000000007) { if (r == 0) return 1; long long fac[n + 1]; fac[0] = 1; for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } long long ask1(long long y) { cout << 1 << " " << y << endl; long long x; cin >> x; return x; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; long long cccc = t; while (t--) { long long n; cin >> n; vector<long long> in(n); for (long long i = 0; i <= n - 1; i++) { cin >> in[i]; } sort(in.begin(), in.end()); reverse(in.begin(), in.end()); for (long long i = 0; i <= n - 1; i++) { cout << in[i] << " "; }; cout << "" << "\n"; } }
#include <bits/stdc++.h> using namespace std; int get_n(int n) { int count = 0; while (n > 0) { n = n / 10; count++; } return count; } int get_num(int a, int p) { int num; p--; while (p >= 0) num += (a * pow(10, p)); return num; } int main() { int t, n, sum, k, digits, ele; cin >> t; while (t--) { cin >> n; vector<int> v; while (n--) { cin >> ele; v.push_back(ele); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[110]; bool cmp(int a, int b) { return a > b; } int main() { int n, m, T, i, j, k; cin >> T; while (T--) { cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, cmp); for (i = 0; i < n; i++) { printf(i == 0 ? "%d" : " %d", a[i]); } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int i; int arr[n]; vector<int> ans; vector<int> then; set<int> in; for (i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n, greater<int>()); for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; void xx(int *a, int n) { for (int j = 1; j < n; ++j) for (int i = j; i <= n; ++i) if (a[j] < a[i]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, n; cin >> t; for (int i = 1; i <= t; ++i) { cin >> n; int a[n]; for (int j = 1; j <= n; ++j) cin >> a[j]; xx(a, n); for (int j = 1; j <= n; ++j) cout << a[j] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> int main() { int t, n; std::cin >> t; for (int i = 0; i < t; ++i) { std::cin >> n; std::vector<int> v(n); for (int j = 0; j < n; ++j) { std::cin >> v[j]; } std::sort(v.begin(), v.end(), std::greater<int>()); for (int k : v) { std::cout << k << " "; } std::cout << std::endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n, greater<int>()); for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n; cin >> t; while (t > 0) { cin >> n; vector<long long int> arr; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; arr.push_back(x); } sort(arr.begin(), arr.end(), greater<long long int>()); for (long long int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; t--; } return 0; }
#include <bits/stdc++.h> using namespace std; int t, n; int main() { cin >> t; while (t--) { cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.rbegin(), a.rend()); for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; long long t[4 * N]; long long mod[4 * N]; const long long Inf = 1e18; void update(int v, long long x) { t[v] += x; mod[v] += x; } void push(int v) { if (mod[v]) { update(2 * v + 1, mod[v]); update(2 * v + 2, mod[v]); mod[v] = 0; } } void build(int l, int r, int v) { if (l == r) { t[v] = Inf; } else { int m = (l + r) >> 1; build(l, m, 2 * v + 1); build(m + 1, r, 2 * v + 2); } } void modify(int ql, int qr, long long val, int l, int r, int v) { if (qr < l || r < ql) return; if (ql <= l && r <= qr) { update(v, val); } else { push(v); int m = (l + r) >> 1; modify(ql, qr, val, l, m, 2 * v + 1); modify(ql, qr, val, m + 1, r, 2 * v + 2); } } long long get(int id, int l, int r, int v) { if (l == r) return t[v]; push(v); int m = (l + r) >> 1; if (id <= m) return get(id, l, m, 2 * v + 1); else return get(id, m + 1, r, 2 * v + 2); } void set_min(int id, long long val, int l, int r, int v) { if (l == r) { t[v] = min(t[v], val); } else { push(v); int m = (l + r) >> 1; if (id <= m) set_min(id, val, l, m, 2 * v + 1); else set_min(id, val, m + 1, r, 2 * v + 2); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> p(n); for (int i = 0; i < n; ++i) cin >> p[i]; int m; cin >> m; vector<int> b(m); for (int i = 0; i < m; ++i) { cin >> b[i]; } { int uk = 0; for (int i = 0; i < n; ++i) { if (uk < m && a[i] == b[uk]) ++uk; } if (uk != m) { cout << "NO\n"; return 0; } else { cout << "YES\n"; } } vector<int> pos(n + 1, -1); for (int i = 0; i < m; ++i) { pos[b[i]] = i; } build(0, n, 0); set_min(0, 0, 0, n, 0); for (int i = 0; i < n; ++i) { if (pos[a[i]] != -1) { long long val = get((pos[a[i]] == 0 ? 0 : b[pos[a[i]] - 1]), 0, n, 0); modify(0, a[i] - 1, p[i], 0, n, 0); if (p[i] < 0) { modify(a[i], n, p[i], 0, n, 0); } set_min(a[i], val, 0, n, 0); } else { modify(0, a[i] - 1, p[i], 0, n, 0); if (p[i] < 0) { modify(a[i], n, p[i], 0, n, 0); } } } cout << get(b[m - 1], 0, n, 0) << '\n'; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void checkmin(T &a, const T &b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, const T &b) { if (b > a) a = b; } const long long inf = 1e18 + 10; const long long infi = 1e15; const int maxn = 5e5 + 10; long long bit[maxn]; int n, m; void add(int i, long long x) { while (i <= m) { bit[i] += x; i += i & -i; } } long long sum(int i) { long long res = 0ll; while (i) { res += bit[i]; i -= i & -i; } return res; } long long a[maxn], b[maxn], p[maxn]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for (int i = 0; i < n; i++) scanf("%lld", &p[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%lld", &b[i]); add(1, -inf); for (int i = 0; i < n; i++) { int low = 1, high = m + 1; while (low < high) { int mid = (low + high) >> 1; if (b[mid] < a[i]) low = mid + 1; else high = mid; } if (low == m + 1) continue; if (p[i] > 0) add(low, p[i]); if (b[low] == a[i]) { if (sum(low - 1) + p[i] > sum(low)) { long long d = sum(low - 1) + p[i] - sum(low); add(low, d); add(low + 1, -d); } } } long long res = sum(m); if (res < -infi) { printf("NO\n"); return 0; } printf("YES\n"); long long ans = 0ll; for (int i = 0; i < n; i++) { ans += p[i]; } printf("%lld\n", ans - res); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, t; long long a[500005], p[500005], b[500005]; long long l, r; void add(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; } void dec(int &a, int b) { a -= b; if (a < 0) a += 1000000007; } int pow_mod(int a, int i) { int s = 1; while (i) { if (i & 1) s = 1LL * s * a % 1000000007; a = 1LL * a * a % 1000000007; i >>= 1; } return s; } struct segtree { long long lazy[4 * 500005], val[4 * 500005]; void pushup(int k) { val[k] = min(val[k * 2], val[k * 2 + 1]); } void add(int k, long long v) { val[k] += v; lazy[k] += v; } void pushdown(int k) { for (int i = k * 2; i <= k * 2 + 1; i++) add(i, lazy[k]); lazy[k] = 0; } void build(int k, int l, int r) { lazy[k] = 0; if (l == r) { if (l == 0) val[k] = 0; else val[k] = 1000000000000000000LL; return; } int mid = (l + r) / 2; build(k * 2, l, mid); build(k * 2 + 1, mid + 1, r); pushup(k); } void update(int k, int l, int r, int x, int y, long long v) { if (l > y || x > r) return; if (l >= x && r <= y) { add(k, v); return; } pushdown(k); int mid = (l + r) / 2; update(k * 2, l, mid, x, y, v); update(k * 2 + 1, mid + 1, r, x, y, v); pushup(k); } long long query(int k, int l, int r, int pos) { if (l == r) return val[k]; pushdown(k); int mid = (l + r) / 2; if (pos <= mid) return query(k * 2, l, mid, pos); else return query(k * 2 + 1, mid + 1, r, pos); } } seg; vector<int> ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= n; i++) scanf("%lld", &p[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%lld", &b[i]); b[m + 1] = 1000000000000000000LL; seg.build(1, 0, m); for (int i = 1; i <= n; i++) { int id = lower_bound(b, b + m + 2, a[i]) - b; if (p[i] < 0) seg.update(1, 0, m, id, id, p[i]); if (a[i] == b[id]) { long long x = seg.query(1, 0, m, id - 1), y = seg.query(1, 0, m, id); if (x < y) seg.update(1, 0, m, id, id, x - y); } seg.update(1, 0, m, 0, id - 1, p[i]); if (p[i] < 0) seg.update(1, 0, m, id + 1, m, p[i]); } long long ans = seg.query(1, 0, m, m); if (ans >= 10000000000000000LL) puts("NO"); else { puts("YES"); printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 500; const int M = 1e6 + 500; const long long INF = 1e18; const int MOD = 1e9 + 7; const int LOG = 20; const int ALP = 30; const int BASE = 31337; const double EPS = 1e-9; const double PI = 3.1415926535; const int OFF = (1 << 19); long long tour[2 * OFF]; int A[N], p[N], B[N]; int n, m; void update(int i, int a, int b, int lo, int hi, long long x) { if (lo <= a && b <= hi) { tour[i] += x; return; } if (a > hi || b < lo) return; update(2 * i, a, (a + b) / 2, lo, hi, x); update(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x); } long long query(int i) { long long ret = 0; for (i = i + OFF; i; i /= 2) ret += tour[i]; return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", A + i); for (int i = 0; i < n; i++) scanf("%d", p + i); scanf("%d", &m); for (int i = 0; i < m; i++) scanf("%d", B + i); tour[1] = INF, tour[OFF] = -INF; int postao = 0; for (int i = 0; i < n; i++) { int kol = lower_bound(B, B + m, A[i]) - B; long long pos = query(kol); if (p[i] >= 0) update(1, 0, OFF - 1, 0, kol - (B[kol] == A[i]), p[i]); else update(1, 0, OFF - 1, 0, m, p[i]); if (kol != m && B[kol] == A[i]) { if (query(kol + 1) > pos) { update(1, 0, OFF - 1, kol + 1, kol + 1, pos - query(kol + 1)); } if (kol + 1 == postao + 1) postao++; } } if (postao < m) printf("NO\n"); else printf("YES\n%lld\n", query(m)); return 0; }
#include <bits/stdc++.h> const int N = 5e5 + 10; const long long inf = 1e16; using namespace std; long long n, m, a[N], b[N], p[N]; long long f[N], c[N], ans, sum; long long read() { long long a, f = 1; char c; c = getchar(); while (!isdigit(c)) { f = c == '-' ? -1 : f; c = getchar(); } a = c - '0'; c = getchar(); while (isdigit(c)) { a = (a << 1) + (a << 3) + c - '0'; c = getchar(); } return a * f; } long long lowbit(long long i) { return i & (-i); } void add(long long i, long long k) { while (i <= n) { c[i] += k; i += lowbit(i); } } long long query(long long i) { long long res = 0; while (i > 0) { res += c[i]; i -= lowbit(i); } return res; } int main() { long long i, j, now; n = read(); for (i = 1; i <= n; i++) a[i] = read(); for (i = 1; i <= n; i++) { p[i] = read(); sum += p[i]; } m = read(); for (i = 1; i <= m; i++) b[i] = read(); add(1, -inf); for (i = 1; i <= n; i++) { j = lower_bound(b + 1, b + m + 1, a[i]) - b; if (j <= m && a[i] == b[j]) { if (j == 1) f[i] = p[i]; else f[i] = query(b[j - 1]) + p[i]; } else f[i] = -inf; if (p[i] > 0) add(a[i], p[i]); now = query(a[i]); if (f[i] > now) { add(a[i], f[i] - now); add(a[i] + 1, -(f[i] - now)); } } ans = query(b[m]); if (ans > -1e15) printf("YES\n%lld\n", sum - ans); else printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& out, vector<T>& v) { out << "["; for (auto k : v) out << k << " "; out << "]" << "\n"; return out; } template <class T> ostream& operator<<(ostream& out, set<T> s) { out << "{"; for (auto k : s) out << k << " "; out << "}" << "\n"; return out; } template <class T, class U> ostream& operator<<(ostream& out, pair<T, U> p) { out << "[ " << p.first << " , " << p.second << " ] "; return out; } template <class T, class U> istream& operator>>(istream& in, pair<T, U>& p) { in >> p.first >> p.second; return in; } const int largo = 1 << 21; long long st[largo]; int L[largo], R[largo]; void Propagar(int nod) { if (nod * 2 >= largo) return; st[nod * 2] += st[nod]; st[nod * 2 + 1] += st[nod]; st[nod] = 0; } long long Punto(int nod, int x) { if (L[nod] == R[nod]) return st[nod]; Propagar(nod); if (x < L[nod * 2 + 1]) return Punto(nod * 2, x); else return Punto(nod * 2 + 1, x); } void Suma(int nod, int lq, int rq, long long s) { if (lq <= L[nod] && R[nod] <= rq) { st[nod] += s; return; } if (lq > R[nod] || rq < L[nod]) return; Propagar(nod); Suma(nod * 2, lq, rq, s); Suma(nod * 2 + 1, lq, rq, s); } void Cambio(int nod, int x, long long v, bool pisar) { if (L[nod] == R[nod] && L[nod] == x) { if (pisar) st[nod] = v; else st[nod] = min(st[nod], v); return; } if (L[nod] > x || R[nod] < x) return; Propagar(nod); Cambio(nod * 2, x, v, pisar); Cambio(nod * 2 + 1, x, v, pisar); } int n, app[largo], a[largo], b[largo], p[largo]; bitset<largo> valid; int main() { cin.tie(0); cin.sync_with_stdio(0); valid.reset(); for (int i = int(largo / 2); i < int(largo); i++) L[i] = R[i] = i - largo / 2; for (int i = int(largo / 2) - 1; i >= 0; i--) L[i] = L[i * 2], R[i] = R[i * 2 + 1]; cin >> n; for (int i = 0; i < int(n); i++) cin >> a[i]; for (int i = 0; i < int(n); i++) cin >> p[i]; memset(app, -1, sizeof(app)); int m; cin >> m; for (int i = 0; i < int(m); i++) { cin >> b[i + 1]; app[b[i + 1]] = i + 1; } valid[0] = true; for (int i = 0; i < int(n); i++) { int ind = app[a[i]]; if (ind != -1 && valid[b[ind - 1]]) { long long pre = Punto(1, b[ind - 1]); if (p[i] > 0) { Suma(1, 0, a[i] - 1, p[i]); } else { Suma(1, 0, largo / 2 - 1, p[i]); } Cambio(1, a[i], pre, !valid[a[i]]); valid[a[i]] = true; } else { if (p[i] > 0) { Suma(1, 0, a[i] - 1, p[i]); } else { Suma(1, 0, largo / 2 - 1, p[i]); } } } if (valid[b[m]]) { cout << "YES\n"; cout << Punto(1, b[m]) << "\n"; } else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; struct BIT { vector<long long> tree; BIT(int n) : tree(n + 2) {} void upd(int p, long long v) { for (int i = ++p; i < tree.size(); i += i & -i) tree[i] += v; } long long get(int p) { long long sum = 0; for (int i = ++p; i; i -= i & -i) sum += tree[i]; return sum; } long long query(int l, int r) { if (r < l) return 0; return get(r) - get(l - 1); } }; vector<int> adj[N], q[N]; int Prev[N], a[N], p[N], b[N]; long long bro[2][N], par[2][N]; long long dp[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; adj[a[i]].push_back(i); } for (int i = 1; i <= n; i++) cin >> p[i]; int m; cin >> m; for (int i = 1; i <= m; i++) { cin >> b[i]; Prev[b[i]] = b[i - 1]; } for (int i = 1; i <= n; i++) { if (!Prev[a[i]]) continue; auto &v = adj[Prev[a[i]]]; auto it = lower_bound(v.begin(), v.end(), i); if (it != v.begin()) { it = prev(it); q[*it].push_back(i); } } memset(dp, '?', sizeof dp); BIT bit(n); for (int e = n; e >= 1; e--) { auto &v = adj[e]; for (int j = 0; j < v.size(); j++) { int i = v[j]; for (int x : q[i]) par[0][x] = bit.query(i + 1, x - 1); if (a[i] == b[m]) dp[i] = bit.query(i + 1, n); if (j != v.size() - 1) bro[0][v[j + 1]] = bit.query(i + 1, v[j + 1]); } for (int j : v) bit.upd(j, p[j]); } bit = BIT(n); for (int e = 1; e <= n; e++) { auto &v = adj[e]; for (int j : v) if (p[j] < 0) bit.upd(j, p[j]); for (int j = 0; j < v.size(); j++) { int i = v[j]; for (int x : q[i]) par[1][x] = bit.query(i + 1, x - 1); if (a[i] == b[m]) { dp[i] += bit.query(i + 1, n); } if (j != v.size() - 1) bro[1][v[j + 1]] = bit.query(i + 1, v[j + 1]); } } long long ans = 2e18; for (int i = n; i >= 1; i--) { if (Prev[a[i]]) { auto &v = adj[Prev[a[i]]]; auto it = lower_bound(v.begin(), v.end(), i); if (it != v.begin()) { it = prev(it); long long v = dp[i] + par[0][i] + par[1][i]; dp[*it] = min(dp[*it], v); } } auto &v = adj[a[i]]; auto it = lower_bound(v.begin(), v.end(), i); if (it != v.begin()) { it = prev(it); long long v = dp[i] + bro[0][i] + bro[1][i]; dp[*it] = min(dp[*it], v); } } long long sum = 0; for (int i = 1; i <= n; i++) { if (a[i] == b[1]) ans = min(ans, dp[i] + sum); sum += p[i]; } if (ans >= 1e17) { cout << "NO"; } else { cout << "YES\n"; cout << ans; } }
#include <bits/stdc++.h> using namespace std; const int maxx = 500001; const long long inf = 1e18; int n, m, arr[maxx], brr[maxx], cst[maxx]; long long fen[maxx], dp[maxx]; map<int, vector<int> > mp; set<pair<int, int> > st; void add(int in, int val) { while (in <= n) { fen[in] += val; in += (in & -in); } } long long get(int in) { long long ret = 0; while (in > 0) { ret += fen[in]; in -= (in & -in); } return ret; } long long query(int l, int r) { return get(r) - get(l - 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i], mp[arr[i]].push_back(i), st.insert({arr[i], i}); for (int i = 1; i <= n; i++) cin >> cst[i], dp[i] = -inf; cin >> m; for (int i = 1; i <= m; i++) cin >> brr[i]; for (auto it : mp[brr[1]]) dp[it] = cst[it]; while (!st.empty() && (*st.begin()).first <= brr[1]) { int x = (*st.begin()).second; if (cst[x] > 0) add(x, cst[x]); st.erase(st.begin()); } for (int i = 1; i < m; i++) { int x = brr[i], y = brr[i + 1]; vector<int> cur; for (auto it : mp[x]) cur.push_back(it); for (auto it : mp[y]) cur.push_back(it); sort(cur.begin(), cur.end()); long long bst = -inf; int lst = 0; for (auto it : cur) { bst += query(lst + 1, it); lst = it; if (arr[it] == x) bst = max(bst, dp[it]); else dp[it] = bst + cst[it]; } while (!st.empty() && (*st.begin()).first <= y) { int x = (*st.begin()).second; if (cst[x] > 0) add(x, cst[x]); st.erase(st.begin()); } } long long res = -inf; for (int i = 1; i <= n; i++) { if (arr[i] == brr[m] && dp[i] != -inf) res = max(res, dp[i] + query(i + 1, n)); } if (res < -1e15) cout << "NO" << '\n'; else { res = -res; cout << "YES" << '\n'; for (int i = 1; i <= n; i++) res += cst[i]; cout << res << '\n'; } }
#include <bits/stdc++.h> using namespace std; template <typename T> struct segTree { private: int n; vector<T> mx, mn, lazySet, sum, lazyAdd; void update(int i) { mx[i] = max(mx[2 * i], mx[2 * i + 1]); mn[i] = min(mn[2 * i], mn[2 * i + 1]); sum[i] = sum[2 * i] + sum[2 * i + 1]; } void init(int l, int r, int i, vector<T> &vals) { if (l == r) { mn[i] = mx[i] = sum[i] = vals[l]; return; } int m = (l + r) / 2; init(l, m, 2 * i, vals); init(m + 1, r, 2 * i + 1, vals); update(i); } void prop(int i, int l, int m, int r) { if (lazySet[i] != numeric_limits<T>::max()) { mx[2 * i] = mx[2 * i + 1] = mn[2 * i] = mn[2 * i + 1] = lazySet[2 * i] = lazySet[2 * i + 1] = lazySet[i]; sum[2 * i] = (long long)(m - l + 1) * lazySet[i]; sum[2 * i + 1] = (long long)(r - m) * lazySet[i]; lazyAdd[2 * i] = 0; lazyAdd[2 * i + 1] = 0; lazySet[i] = numeric_limits<T>::max(); } if (lazyAdd[i]) { mx[2 * i] += lazyAdd[i]; mx[2 * i + 1] += lazyAdd[i]; mn[2 * i] += lazyAdd[i]; mn[2 * i + 1] += lazyAdd[i]; sum[2 * i] += (long long)(m - l + 1) * lazyAdd[i]; sum[2 * i + 1] += (long long)(r - m) * lazyAdd[i]; lazyAdd[2 * i] += lazyAdd[i]; lazyAdd[2 * i + 1] += lazyAdd[i]; lazyAdd[i] = 0; } } void setSingle(int pos, T x, int l, int r, int i) { if (l == r) { mx[i] = x; mn[i] = x; sum[i] = x; return; } int m = (l + r) / 2; prop(i, l, m, r); if (pos <= m) setSingle(pos, x, l, m, 2 * i); else setSingle(pos, x, m + 1, r, 2 * i + 1); update(i); } void addSingle(int pos, T x, int l, int r, int i) { if (l == r) { mx[i] += x; mn[i] += x; sum[i] += x; return; } int m = (l + r) / 2; prop(i, l, m, r); if (pos <= m) addSingle(pos, x, l, m, 2 * i); else addSingle(pos, x, m + 1, r, 2 * i + 1); update(i); } void setRange(int qs, int qe, T x, int l, int r, int i) { if (qs > r || qe < l) return; if (qs <= l && qe >= r) { lazyAdd[i] = 0; mx[i] = mn[i] = lazySet[i] = x; sum[i] = (long long)(r - l + 1) * x; return; } int m = (l + r) / 2; prop(i, l, m, r); setRange(qs, qe, x, l, m, 2 * i); setRange(qs, qe, x, m + 1, r, 2 * i + 1); update(i); } void addRange(int qs, int qe, T x, int l, int r, int i) { if (qs > r || qe < l) return; if (qs <= l && qe >= r) { lazyAdd[i] += x; mx[i] += x; mn[i] += x; sum[i] += (long long)(r - l + 1) * x; return; } int m = (l + r) / 2; prop(i, l, m, r); addRange(qs, qe, x, l, m, 2 * i); addRange(qs, qe, x, m + 1, r, 2 * i + 1); update(i); } int getMx(int qs, int qe, int l, int r, int i) { if (qs > r || qe < l) return numeric_limits<T>::min(); if (qs <= l && qe >= r) return mx[i]; int m = (l + r) / 2; prop(i, l, m, r); return max(getMx(qs, qe, l, m, 2 * i), getMx(qs, qe, m + 1, r, 2 * i + 1)); } int getMn(int qs, int qe, int l, int r, int i) { if (qs > r || qe < l) return numeric_limits<T>::max(); if (qs <= l && qe >= r) return mn[i]; int m = (l + r) / 2; prop(i, l, m, r); return min(getMn(qs, qe, l, m, 2 * i), getMn(qs, qe, m + 1, r, 2 * i + 1)); } long long getSum(int qs, int qe, int l, int r, int i) { if (qs > r || qe < l) return 0; if (qs <= l && qe >= r) return sum[i]; int m = (l + r) / 2; prop(i, l, m, r); return getSum(qs, qe, l, m, 2 * i) + getSum(qs, qe, m + 1, r, 2 * i + 1); } public: void init(int N) { n = N; int pwr = 1; while (pwr < 2 * n) pwr *= 2; mx.resize(pwr); mn.resize(pwr); sum.resize(pwr); lazyAdd.resize(pwr); lazySet.resize(pwr); for (int i = 0; i < pwr; i++) mx[i] = mn[i] = sum[i] = lazyAdd[i] = 0, lazySet[i] = numeric_limits<T>::max(); } void init(vector<T> &vals) { init(vals.size()); init(0, n - 1, 1, vals); } void singleSet(int pos, T x) { setSingle(pos, x, 0, n - 1, 1); } void singleAdd(int pos, T x) { addSingle(pos, x, 0, n - 1, 1); } void rangeSet(int qs, int qe, T x) { setRange(qs, qe, x, 0, n - 1, 1); } void rangeAdd(int qs, int qe, T x) { addRange(qs, qe, x, 0, n - 1, 1); } int getMax(int qs, int qe) { return getMx(qs, qe, 0, n - 1, 1); } int getMin(int qs, int qe) { return getMn(qs, qe, 0, n - 1, 1); } long long getSum(int qs, int qe) { return getSum(qs, qe, 0, n - 1, 1); } }; segTree<long long> svi, neg; const long long oo = LLONG_MAX / 2; int main() { int n; scanf("%i", &n); vector<int> a(n + 2), pr(n + 2), b; for (int i = 1; i <= n; i++) scanf("%i", &a[i]); for (int i = 1; i <= n; i++) scanf("%i", &pr[i]); int m; scanf("%i", &m); b.resize(m + 1); vector<int> pos(n + 1, -1); for (int i = 1; i <= m; i++) scanf("%i", &b[i]), pos[b[i]] = i; a[n + 1] = b[m]; vector<int> last(n + 1, -1); last[0] = 0; vector<long long> dp(n + 2), veci(n + 2), manji(n + 2); svi.init(n + 1); neg.init(n + 1); for (int i = 1; i <= n + 1; i++) { dp[i] = oo; if (pos[a[i]] == -1) { svi.singleAdd(a[i], pr[i]); if (pr[i] < 0) neg.singleAdd(a[i], pr[i]); continue; } veci[i] = svi.getSum(a[i] + 1, n); manji[i] = neg.getSum(1, a[i] - 1); int p = pos[a[i]]; if (last[b[p]] != -1) { int g = last[b[p]]; long long tada = dp[g] + veci[i] - veci[g] + manji[i] - manji[g]; if (pr[i] < 0) tada += pr[i]; dp[i] = min(dp[i], tada); } if (i != n + 1 && last[b[p - 1]] != -1) { int g = last[b[p - 1]]; int koji = b[p - 1]; long long tada = dp[g] + svi.getSum(koji + 1, n) - veci[g] + neg.getSum(1, koji - 1) - manji[g]; dp[i] = min(dp[i], tada); } svi.singleAdd(a[i], pr[i]); if (pr[i] < 0) neg.singleAdd(a[i], pr[i]); last[a[i]] = i; } if (dp[n + 1] >= oo / 2) printf("NO\n"); else printf("YES\n%lld\n", dp[n + 1]); return 0; }
#include <bits/stdc++.h> using namespace std; constexpr long long int MOD = 998244353; constexpr double EPS = 1e-9; long long int N, M, K, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; vector<int> v(N); for (auto &i : v) cin >> i; vector<long long int> cost(N); long long int ans = 0; for (auto &i : cost) { cin >> i; ans += i; } cin >> M; vector<int> w(1, 0); for (int i = 0; i < M; i++) { cin >> K; w.push_back(K); } for (int i = 1; i < M; i++) { if (w[i] <= w[i - 1]) { cout << "NO\n"; return 0; } } map<int, int> converter; for (int i = 0; i <= M; i++) { converter[w[i]] = i; } int cnt = 1; for (int i = 0; i < N; i++) { if (v[i] == w[cnt]) cnt++; if (cnt == M + 1) break; } if (cnt < M + 1) { cout << "NO\n"; return 0; } map<int, long long int> minus; map<int, long long int> ret; for (int i = 0; i <= w.size(); i++) ret[i] = MOD * MOD; ret[w.size()] = ans; for (int i = N - 1; i >= 0; i--) { auto it = lower_bound(w.begin(), w.end(), v[i]) - w.begin(); if (converter.find(v[i]) == converter.end()) { minus[it - 1] += max(0LL, cost[i]); continue; } ret[it] = min(ret[it], ret[it + 1] - minus[it - 1] - cost[i]); minus[it - 1] += max(cost[i], 0LL); } cout << "YES\n"; cout << ret[1] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vii = vector<ii>; const ll MOD = 998244353; const ll INF = 1e15 + 9; const int MAXN = 1000006; ll n, m, a[MAXN], p[MAXN], b[MAXN]; ll rev[MAXN]; vll pos[MAXN]; vll sum[MAXN]; ll dp[MAXN], negsum[MAXN]; ll isum(ll val, ll R) { ll index = lower_bound(pos[val].begin(), pos[val].end(), R) - pos[val].begin(); return sum[val][index]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (ll i = 1; i <= n; ++i) { cin >> a[i]; } for (ll i = 1; i <= n; ++i) { cin >> p[i]; } cin >> m; for (ll i = 1; i <= m; ++i) { cin >> b[i]; } a[n + 1] = n + 1; b[m + 1] = n + 1; ++n, ++m; for (ll i = 0; i <= m; ++i) { sum[2 * i + 1] = sum[2 * i] = vll(1); } for (ll i = 0; i <= n; ++i) { ll bi = lower_bound(b, b + m, a[i]) - b; a[i] = 2 * bi - (b[bi] != a[i]); pos[a[i]].push_back(i); ll prevsum = sum[a[i]].back(); sum[a[i]].push_back(prevsum + max(p[i], 0ll)); negsum[i + 1] = negsum[i] + min(p[i], 0ll); } for (ll i = 1; i <= n; ++i) { if (a[i] & 1) { dp[i] = INF; continue; } auto it = lower_bound(pos[a[i] - 2].begin(), pos[a[i] - 2].end(), i); if (it == pos[a[i] - 2].begin()) { dp[i] = INF; continue; } --it; dp[i] = dp[*it] + isum(a[i] - 1, i) + isum(a[i], i) + negsum[i] - negsum[*it + 1]; it = lower_bound(pos[a[i]].begin(), pos[a[i]].end(), i); if (it != pos[a[i]].begin()) { --it; dp[i] = min(dp[i], dp[*it] + negsum[i + 1] - negsum[*it + 1]); } } if (dp[n] >= INF / 2) { cout << "NO\n"; } else { cout << "YES\n" << dp[n] << "\n"; } }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, m, a[N], b[N], con[N], p[N]; long long c[N]; int lowbit(int x) { return x & (-x); } void add(int x, long long v) { for (int i = x + 1; i <= m + 1; i += lowbit(i)) c[i] += v; } long long get(int x) { long long ans = 0; for (x++; x; x -= lowbit(x)) ans += c[x]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), con[b[i]] = i; add(1, 1e18); for (int i = 1; i <= n; i++) { long long v, nv; if (con[a[i]]) v = get(con[a[i]] - 1); add(0, p[i]); if (p[i] > 0) add(lower_bound(b + 1, b + m + 1, a[i]) - b, -p[i]); if (con[a[i]]) { nv = get(con[a[i]]); if (v < nv) add(con[a[i]], v - nv), add(con[a[i]] + 1, nv - v); } } long long ans = get(m); if (ans > 1e16) puts("NO"); else printf("YES\n%lld\n", ans); return 0; }