Problem p02662 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

void solve(vector<vector<pll>> &K, int x, ll kiek, ll sukiek, ll &liek) {
  kiek = kiek % liek;
  for (int i = 0; i < (int)K[x].size(); i++) {
    if (K[x][i].second == sukiek) {
      K[x][i].first += kiek;
      K[x][i].first = K[x][i].first % liek;
      return;
    }
  }
  K[x].push_back({kiek % liek, sukiek});
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, s;
  cin >> n >> s;
  vector<ll> A;
  vector<ll> D(n + 1);
  ll liek = 998244353;
  D[0] = 1;
  ll d = 2;
  for (int i = 1; i <= n; i++) {
    D[i] = d;
    d = (2 * d) % liek;
  }
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a <= s) {
      A.push_back(a);
    }
  }
  vector<ll> K(s + 1);
  for (int i = 0; i < (int)A.size(); i++) {
    for (int k = s - A[i]; k >= 0; k--) {
      ll x;
      if (K[k] % 2 == 0) {
        x = K[k] / 2;
      } else {
        x = (K[k] + liek) / 2;
      }
      K[k + A[i]] += x;
      K[k + A[i]] = K[k + A[i]] % liek;
    }
    K[A[i]] += D[n - 1];
    K[A[i]] = K[A[i]] % liek;
  }

  cout << K[s] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

void solve(vector<vector<pll>> &K, int x, ll kiek, ll sukiek, ll &liek) {
  kiek = kiek % liek;
  for (int i = 0; i < (int)K[x].size(); i++) {
    if (K[x][i].second == sukiek) {
      K[x][i].first += kiek;
      K[x][i].first = K[x][i].first % liek;
      return;
    }
  }
  K[x].push_back({kiek % liek, sukiek});
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, s;
  cin >> n >> s;
  vector<ll> A;
  vector<ll> D(n + 1);
  ll liek = 998244353;
  D[0] = 1;
  ll d = 2;
  for (int i = 1; i <= n; i++) {
    D[i] = d;
    d = (2 * d) % liek;
  }
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a <= s) {
      A.push_back(a);
    }
  }
  vector<vector<pll>> K(s + 1);
  for (int i = 0; i < (int)A.size(); i++) {
    for (int k = s - A[i]; k >= 0; k--) {
      for (int j = 0; j < (int)K[k].size(); j++) {
        solve(K, A[i] + k, K[k][j].first, K[k][j].second + 1, liek);
      }
    }
    solve(K, A[i], 1, 1, liek);
  }
  ll ats = 0;
  for (int i = 0; i < (int)K[s].size(); i++) {
    // cout<<K[s][i].first<<" "<<K[s][i].second<<endl;
    ats = ats + K[s][i].first * D[n - K[s][i].second];
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}