1914D - Three Activities - CodeForces Solution


brute force dp implementation sortings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define fr first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x)
{
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define debug(x...)
#endif

ll power(ll base, ll p)
{
  ll res = 1;
  base = base % M;
  while (p > 0)
  {
    if (p & 1)
    {
      res = (res * base) % M;
      p--;
    }
    base = (base * base) % M;
    p >>= 1;
  }
  return res;
}

void solve()
{
  int n;
  cin >> n;
  vector<pair<ll, int>> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
  {
    cin >> a[i].first;
    a[i].second = i;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> b[i].first;
    b[i].second = i;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> c[i].first;
    c[i].second = i;
  }
  ll maxi = INT_MIN;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  vector<pair<ll, int>> A(3), B(3), C(3);
  A[0] = a[n - 1];
  A[1] = a[n - 2];
  A[2] = a[n - 3];
  B[0] = b[n - 1];
  B[1] = b[n - 2];
  B[2] = b[n - 3];
  C[0] = c[n - 1];
  C[1] = c[n - 2];
  C[2] = c[n - 3];
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      for (int k = 0; k < 3; k++)
      {
        if (A[i].second == B[j].second || B[j].second == C[k].second || C[k].second == A[i].second)
        {
          continue;
        }
        else
        {
          maxi = max(maxi, A[i].first + B[j].first + C[k].first);
        }
      }
    }
  }
  cout << maxi << "\n";
}

int32_t main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
  auto start = chrono::high_resolution_clock::now();

  int t;
  cin >> t;
  for (int tc = 1; tc <= t; ++tc)
  {
    solve();
  }
  auto stop = chrono::high_resolution_clock::now();
  auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
  cerr << duration.count() / 1000.0 << ' ' << 'm' << 's';

  return 0;
}


Comments

Submit
0 Comments
More Questions

1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops