Submission #1122577

#TimeUsernameProblemLanguageResultExecution timeMemory
1122577barkoloriousBank (IZhO14_bank)C++17
100 / 100
134 ms16848 KiB
// barkolorious - 29 November 2024
// in god, do we trust? 
#include <bits/stdc++.h>
using namespace std;

#define FIN(x) freopen(x ".in", "r", stdin)
#define FOUT(x) freopen(x ".out", "w", stdout)
#define int long long
#define pb  push_back
#define fr  first
#define sc  second
#define __  << " " << 
#define check_bit(a, i) (((a) & (1 << (i))) >> (i))
#define flip_bit(a, i)  ((a) & ~(1 << (i)))

const int N = 2e5 + 5;

void solve () {
  int n, m; cin >> n >> m;

  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];

  int E = (1 << m);
  int dp1[E], dp2[E];
  dp1[0] = dp2[0] = 0;
  for (int s = 1; s < E; s++) dp1[s] = dp2[s] = -1;

  for (int s = 0; s < E; s++) {
    for (int i = 0; i < m; i++) {
      if (!check_bit(s, i)) continue;
      
      int last = flip_bit(s, i);
      if (dp1[last] == -1) continue;

      if (dp2[last] + b[i] == a[dp1[last]]) { 
        dp1[s] = dp1[last] + 1;
        dp2[s] = 0;
      } else if (dp2[last] + b[i] < a[dp1[last]]) { 
        dp1[s] = dp1[last];
        dp2[s] = dp2[last] + b[i];
      }
    }
    if (dp1[s] == n) {
      cout << "YES" << endl;
      return;
    }
  }
  cout << "NO" << endl;
}

/*
-- Sample 1 --
Input:
1 5
8
4 2 5 1 3
Output:
YES

-- Sample 2 --
Input:
2 6
9 10
5 4 8 6 3 11
Output:
NO
*/

/*
g++ -std=c++17 -O2 -Wall -DLOCAL "C:\Users\LENOVO\Desktop\BARKIN\Genel\Programming\Competitive\Questions\oj.uz\IZhO14\IZhO14_bank.cpp" -o _run
*/

int32_t main () {
  #ifndef LOCAL
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  #endif

  #ifdef LOCAL
    clock_t __START__ = clock();
    FILE* __FILE_IN__ = FIN("C:/Users/LENOVO/Desktop/BARKIN/Genel/Programming/Competitive/_run");
    FILE* __FILE_OUT__ = FOUT("C:/Users/LENOVO/Desktop/BARKIN/Genel/Programming/Competitive/_run");
  #endif

  solve();

  #ifdef LOCAL
    fclose(__FILE_IN__);
    fclose(__FILE_OUT__);
    cerr << "Executed in: " << fixed << setprecision(3) << (double) (clock() - __START__) / CLOCKS_PER_SEC << "seconds" << endl;
  #endif

  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...