fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(int argc, char const *argv[])
  4. {
  5. int t;
  6. cin>>t;
  7. while(t--)
  8. {
  9. long long int n,k,sum=0;
  10. cin>>n>>k;
  11. if(n<k)
  12. {
  13. cout<<"no"<<endl;
  14. continue;
  15. }
  16. vector<long long int> inp(n);
  17. for (int i = 0; i < n; ++i)
  18. {
  19. cin>>inp[i];
  20. sum+=inp[i];
  21. }
  22. sort(inp.begin(),inp.end());
  23. // for (int i = 0; i < inp.size(); ++i)
  24. // {
  25. // cout<<inp[i]<<",";
  26. // }
  27. // cout<<endl;
  28. if(sum%k != 0)
  29. {
  30. cout<<"no"<<endl;
  31. continue;
  32. }
  33. long long int each = sum/k;
  34. //cout<<each<<endl;
  35. vector<long long int> st;
  36. for (int i = n-1; i >= 0; --i)
  37. {
  38. if(inp[i] == each || inp[i] == 0) continue;
  39. for (int j = 0; j <= st.size(); ++j)
  40. {
  41. if(j == st.size())
  42. {
  43. st.push_back(inp[i]);
  44. break;
  45. }
  46. else if((st[j] + inp[i]) == each)
  47. {
  48. st.erase(st.begin()+j);
  49. break;
  50. }
  51. else if((st[j] + inp[i]) < each)
  52. {
  53. st[j]+=inp[i];
  54. break;
  55. }
  56. }
  57. }
  58. if(st.empty())
  59. {
  60. cout<<"yes"<<endl;
  61. }
  62. else
  63. {
  64. cout<<"no"<<endl;
  65. }
  66. }
  67. return 0;
  68. }
Success #stdin #stdout 0s 3480KB
stdin
1
6 2
4 9 10 17 17 19
stdout
no