1859B - Olya and Game with Arrays - CodeForces Solution


constructive algorithms greedy sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<long long> vll;
#define vi vector<int>
#define vvi vector<vi>
#define ff first
#define ss second
#define vvl vector<vll>
#define pb push_back
#define ho(a) a.begin(), a.end()
#define trace(arr)                       \
    for (int i = 0; i < arr.size(); i++) \
    {                                    \
        cout << arr[i] << ' ';           \
    }                                    \
    cout << endl;
#define countsetbit(n) __builtin_popcountll(n)
ll aj = LLONG_MAX;
ll mod = 1e9 + 7;
ll mod2 = 998244353;
bool cmp(pr a, pr b)
{
    return a.second < b.second;
}
ll binpow3(ll a, ll b)
{
    ll res = 1;
    a = a % mod;
    while (b > 0)
    {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b = b >> 1;
    }
    return res;
}

string numtostring(ll n)
{
    return bitset<31>(n).to_string();
}

unsigned long stringtonum(string s)
{
    return bitset<31>(s).to_ulong();
}

bool sortbysec(const pair<int, int> &a, const pair<int, int> &b)
{
    return (a.second < b.second);
}

int poww(int a, int b)
{
    int ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (ans * 1LL * a) % mod;
        }
        a = (a * 1LL * a) % mod;
        b >>= 1;
    }
    return ans;
}

int mull(ll a, ll b)
{
    int ans = 0;
    while (b)
    {
        if (b & 1)
        {
            ans = (ans + a) % mod;
        }
        a = (a + a) % mod;
        b >>= 1;
    }
    return ans;
}
bool BipartiteDfs(ll node, vvl &adj, vll &vis)
{
    for (auto it : adj[node])
    {
        if (vis[it] == -1)
        {
            vis[it] = 1 - vis[node];
            if (!BipartiteDfs(it, adj, vis))
            {
                return false;
            }
        }
        else if (vis[node] == vis[it])
        {
            return false;
        }
    }
    return true;
}

bool is_prime(int n)
{
    if (n == 2)
    {
        return true;
    }
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }
    return true;
}

void printPrimeFactors(int n, vll &fac)
{
    while (n % 2 == 0)
    {
        fac.pb(2);
        n = n / 2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            fac.pb(i);
            n = n / i;
        }
    }
    if (n > 2)
        fac.pb(n);
}

bool cmp2(pr &p1, pr &p2)
{
    if (p1.first == p2.first)
        return p1.second < p2.second;

    return p1.first > p2.first;
}
long long nearestPowerOf2(long long N)
{
    long long a = log2(N);

    if (pow(2, a) == N)
        return N;

    return pow(2, a + 1);
}
void solve()
{
    ll n;
    cin >> n;
    map<ll, ll> mp;
    // vll v(n);
    ll mini = LLONG_MAX;
    // int id = 0;
    // vector<vector<ll>> v(n);
    vll ans;
    for (int i = 0; i < n; i++)
    {
        int n1;
        cin >> n1;
        vll v1(n1);
        int id = 0;
        for (int j = 0; j < n1; j++)
        {
            cin >> v1[j];
            // v[i].pb(v1[j]);
            if (mini > v1[j])
            {
                mini = v1[j];
                // id = i;
            }
        }
        ll smallest = LLONG_MAX;
        ll secondM = LLONG_MAX;
        for (int j = 0; j < n1; j++)
        {
            if (v1[j] < smallest)
            {
                smallest = v1[j];
                id = j;
            }
        }
        for (int j = 0; j < n1; j++)
        {
            if (v1[j] < secondM && v1[j] >= smallest && id != j)
            {
                secondM = v1[j];
            }
        }
        // if (secondM == LLONG_MAX)
        // {
        //     ans.pb(smallest);
        // }

        ans.pb(secondM);
    }
    sort(ho(ans));
    reverse(ho(ans));
    ll sum = 0;
    // trace(ans);
    for (int i = 0; i < n - 1; i++)
    {
        sum += ans[i];
    }
    sum += mini;

    cout << sum << "\n";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!