A (Buy K get 1 Free)
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
ll t; cin>>t; while(t--){
ll n,k;cin>>n>>k;string s;ll arr[n];
ll sum=0;map<ll,ll> ms;
for (ll i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);ll pre[n+1];pre[0]=0;
for (ll i=0;i<n;i++){
pre[i+1]=pre[i]+arr[i];
}
ll dp[n];
for (ll i=0;i<=k;i++){
if (i<k) dp[i]=0;
else dp[i]=arr[0];
for (ll j=i+k+1;j<n;j+=(k+1)){
dp[j]=dp[j-k-1]+arr[j-k];
}
}
for (ll i=0;i<n;i++) cout<<pre[i+1]-dp[i]<<" ";cout<<endl;
}
}
D (game-2048)
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod = 998244353;
const int MAX_SCORE = 4000001;
vector<ll> nums;
ll dp[MAX_SCORE + 1];
void combinationSum() {
nums.push_back(4);
for (int i = 1; i <= 22; i++) {
ll k = (nums[i - 1] * 2) + (1LL << (i + 2));
if (k > MAX_SCORE) break;
nums.push_back(k);
}
ll n = nums.size();
memset(dp,0,sizeof(dp));
dp[0] = 1;
for (ll tile : nums) {
for (int weight = tile; weight <= MAX_SCORE; weight++) {
dp[weight] = (dp[weight] + dp[weight - tile]) % mod;
}
}
}
void solve() {
ll y;
cin >> y;
cout << dp[y] << endl;
}
int main() {
combinationSum();
ll t;
cin >> t;
while (t--) {
solve();
}
return 0;
}