1829C - Mr Perfectly Fine - CodeForces Solution


bitmasks bitmasks greedy implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl '\n'
#define nn 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define f0(n) for (int i = 0; i < n; i++)
#define f1(n) for (int i = 1; i < n; i++)
#define all(_a) _a.begin(), _a.end()
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " " << x << endl;
#else
#define debug(x)
#endif
#define rock_lee()                        \
    {                                     \
        ios_base::sync_with_stdio(false); \
        cin.tie(NULL);                    \
    }

void yoyochikichiki()
{
    rock_lee();
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
}
int32_t main()
{
    yoyochikichiki();
    int t;
    cin >> t;
    while (t--)
    {
        int sum = 0, flag = 0;
        int n;
        cin >> n;
        // debug(n);
        vector<int> b;
        vector<int> c;
        vector<int> d;
        for (int i = 0; i < n; i++)
        {
            int a;
            string s;
            cin >> a >> s;

            if (s == "10")
            {
                b.pb(a);
                // debug(b[0]);
            }
            else if (s == "01")
            {
                c.pb(a);
                // debug(c[0]);
            }
            else if (s == "11")
                d.pb(a);
        }
        if (b.size() == 0)
            b.pb(0);
        if (c.size() == 0)
            c.pb(0);
        if (d.size() == 0)
            d.pb(0);
        // for(int i=0;i<b.size();i++) cout<<b[i]<<" ";
        // cout << b[0];

        // cout << endl;
        sort(all(b));
        sort(all(c));
        sort(all(d));
        // debug(b[0]);
        // debug(c[0]);
        // debug(d[0]);
        int x = b[0] + c[0];
     
        if (d[0] == 0)
        {
            if (b[0] != 0 && c[0] != 0)
                cout << x << endl;
            else
                cout << -1 << endl;
        }
        else
        {
            if (b[0] == 0 || c[0] == 0)
                cout << d[0] << endl;
            else
            {
                if (x <= d[0])
                    cout << x << endl;
                else if (x > d[0])
                    cout << d[0] << endl;
                else
                    cout << -1 << endl;
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words