1882C - Card Game - CodeForces Solution


greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <vector>
#define pb push_back
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define ll  long long int 
#define gt greater<ll>()
#define ff first
#define ss second
#define pi acos(-1)
#define ull unsigned long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define  mod 1000000007;
#define bi_one(n)      __builtin_popcount(n)
#define str(a)          a.begin(),a.end()
#define mem(a,b)        memset(a, b, sizeof(a) )
#define max_ele(a,n)    *max_element(a,a+n)
#define min_ele(a,n)    *min_element(a,a+n)
#define fio()                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define sp(n) fixed << setprecision(n)
#define Erase(s) s.erase(unique(s.begin(), s.end()), s.end())
#define fn0 for (i = 0; i < n; i++)
#define fn1 for(i=1; i<=n; i++)
#define upper(v,val) upper_bound(v.begin(), v.end(), val)-v.begin()
#define lower(v,val) lower_bound(v.begin(), v.end(), val)-v.begin()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        (a*(b/gcd(a,b)))
using namespace std;
 ll vv[200007];
 ll modd= 998244353;
const int N=2e5+5;
vector<int> g[N];
bool vis[N];
int n,m,ans=0;
//vector<ll>v,vv;
map<ll,ll>mp;
void print(vector<ll> &v)
{
    for (ll i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << endl;
}

  void fact()
  {
    vv[0]=1;
    for(ll i=1; i<=200005; i++)
    {
        vv[i]=(vv[i-1]*i)%modd;
     }
 }
void solution()
{
    ll n, m,sq,r=0,q, k = 0,l=0, i, j, a=0, b=0,d=0, c = 0, d1 = 0, sum = 0;
    string s,s1="";
    cin>>n;
    vector<ll> v1,v0,v;

    fn0
    {
        cin>>a;
        v.pb(a);
        if(a>=0)
            sum+=a;
    }
    if(v[0]>=0 || n==1 || v[1]<0)
    {
        cout<<sum<<endl;
        return;
    }
    if(v[1]> abs(v[0]))
    {
        sum+=v[0];
        cout<<sum<<endl;
        return;
    }
    sum-=v[1];
    cout<<sum<<endl;
    return;
}

int main()
{
    fio();
   // okk();
    int tt = 1;
   cin >> tt;
  //fact();
   // int x=1;
  
  
    while (tt--)
    {
        //cout<<"Case "<<x<< ": ";
        solution();
        //x++;
    }
}


Comments

Submit
0 Comments
More Questions

505B - Mr Kitayuta's Colorful Graph
1324D - Pair of Topics
157B - Trace
34C - Page Numbers
279A - Point on Spiral
1294D - MEX maximizing
447A - DZY Loves Hash
23B - Party
63D - Dividing Island
1203E - Boxers
1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings