Problem p01717 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

struct Edge {
  int from;
  int to;
  int cost;
};
vector<vector<vector<int>>> *dp;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<Edge>> edges(N);
  vector<vector<int>> haits(N);
  /*for (int i = 0; i < M; ++i) {
          int a, b, c; cin >> a >> b >> c;
          a--; b--;
          if (c) {
                  edges[a].push_back(Edge{ a,b,c });
                  edges[b].push_back(Edge{ b,a,c });
          }
          else {
                  haits[a].push_back(b);
                  haits[b].push_back(a);
          }
  }
  vector<vector<int>>hlines;
  int num = -1;
  vector<bool>oks(N);
  for (int i = 0; i < M; ++i) {
          if (haits[i].size() == 1&&!oks[i]) {
                  oks[i] = true;
                  num++;
                  hlines.push_back(vector<int>());
                  int next = haits[i][0];
                  while (1) {
                          hlines[num].push_back(next);
                          int anext = -1;
                          for (auto e : haits[next]) {
                                  if (e != next) {
                                          anext = e;
                                  }
                          }
                          if (anext == -1)break;
                          next = 1;
                  }
          }
  }*/
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(Edge{a, b, c});
    edges[b].push_back(Edge{b, a, c});
  }
  vector<bool> oks(N);
  vector<vector<int>> line_loves;
  {
    vector<vector<int>> lines;
    int num = -1;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 1 && !oks[i]) {
        oks[i] = true;
        num++;
        lines.push_back(vector<int>());
        line_loves.push_back(vector<int>());
        lines[num].push_back(i);
        line_loves[num].push_back(edges[i][0].cost);
        int next = edges[i][0].to;
        while (1) {
          oks[next] = true;
          int anext = -1;
          for (auto e : edges[next]) {
            if (!oks[e.to]) {
              anext = e.to;
              line_loves[num].push_back(e.cost);
            }
          }
          if (anext == -1)
            break;
          next = anext;
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 0) {
        oks[i] = true;
        lines.push_back(vector<int>());
        line_loves.push_back(vector<int>());
        lines[num].push_back(i);
      }
    }
  }

  vector<vector<int>> circle_loves;
  {
    vector<vector<int>> circles;
    int num = -1;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 2 && !oks[i]) {
        oks[i] = true;
        num++;
        circles.push_back(vector<int>());
        circle_loves.push_back(vector<int>());
        circles[num].push_back(i);
        circle_loves[num].push_back(edges[i][0].cost);
        int next = edges[i][0].to;
        while (1) {
          oks[next] = true;
          circles[num].push_back(next);
          int anext = -1;
          for (auto e : edges[next]) {
            if (!oks[e.to]) {
              anext = e.to;
              circle_loves[num].push_back(e.cost);
            }
          }
          if (anext == -1 || oks[anext])
            break;
          next = anext;
        }
        if (edges[next][0].to == circles[num][0]) {
          circle_loves[num].push_back(edges[next][0].cost);
        } else {
          circle_loves[num].push_back(edges[next][1].cost);
        }
      }
    }
  }

  vector<int> ans(2001, -9e8);
  ans[0] = 0;
  for (auto l : line_loves) {
    const int size_ = l.size() + 1;
    dp = new vector<vector<vector<int>>>(
        2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
    // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
    // vector<int>(2, -9e8)));
    (*dp)[0][0][0] = 0;
    (*dp)[0][1][1] = 0;
    for (int i = 0; i < size_ - 1; ++i) {
      int tar = i & 1, cur = tar ^ 1;
      for (int k = 0; k < size_ + 1; ++k) {
        for (int l = 0; l < 2; ++l) {
          (*dp)[cur][k][l] = -9e8;
        }
      }
      for (int j = 0; j < size_; ++j) {

        if (i == 0)
          assert(tar == 0 && cur == 1);
        if (l[i]) {
          (*dp)[cur][j][0] =
              max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
          (*dp)[cur][j + 1][1] =
              max((*dp)[cur][j + 1][1],
                  max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
        } else {
          (*dp)[cur][j][0] =
              max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
          (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
        }
      }
    }
    vector<int> nowans(size_ + 1, -9e8);
    for (int j = 0; j <= size_; ++j) {
      nowans[j] =
          max((*dp)[(size_ - 1) & 1][j][0], (*dp)[(size_ - 1) & 1][j][1]);
    }

    vector<int> nans(2001, -9e8);
    for (int anew = 0; anew <= 2000; ++anew) {
      for (int from = anew - size_; from <= anew; ++from) {
        if (from < 0)
          continue;
        nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]);
      }
    }
    for (int j = 0; j <= 2000; ++j) {
      ans[j] = max(ans[j], nans[j]);
    }
    free(dp);
  }
  for (auto l : circle_loves) {

    const int size_ = l.size() + 1;
    vector<int> nowans(size_ + 1, -9e8);
    {

      dp = new vector<vector<vector<int>>>(
          2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
      // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
      // vector<int>(2, -9e8)));
      (*dp)[0][0][0] = 0;
      (*dp)[0][1][1] = -9e8;
      for (int i = 0; i < size_ - 1; ++i) {
        int tar = i & 1, cur = tar ^ 1;
        for (int k = 0; k < size_ + 1; ++k) {
          for (int l = 0; l < 2; ++l) {
            (*dp)[cur][k][l] = -9e8;
          }
        }
        for (int j = 0; j < size_; ++j) {

          if (i == 0)
            assert(tar == 0 && cur == 1);
          if (l[i]) {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] =
                max((*dp)[cur][j + 1][1],
                    max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
          } else {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
          }
        }
      }
      for (int j = 0; j <= size_; ++j) {
        nowans[j] = (*dp)[(size_ - 1) & 1][j][0];
      }
    }
    {

      dp = new vector<vector<vector<int>>>(
          2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
      // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
      // vector<int>(2, -9e8)));(*dp)[0][0][0] = -9e8;
      (*dp)[0][1][1] = 0;
      for (int i = 0; i < size_ - 1; ++i) {
        int tar = i & 1, cur = tar ^ 1;
        for (int k = 0; k < size_ + 1; ++k) {
          for (int l = 0; l < 2; ++l) {
            (*dp)[cur][k][l] = -9e8;
          }
        }
        for (int j = 0; j < size_; ++j) {

          if (i == 0)
            assert(tar == 0 && cur == 1);
          if (l[i]) {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] =
                max((*dp)[cur][j + 1][1],
                    max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
          } else {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
          }
        }
      }
      for (int j = 1; j <= size_; ++j) {
        nowans[j - 1] = max(nowans[j - 1], (*dp)[(size_ - 1) & 1][j][1]);
      }
    }
    vector<int> nans(2001, -9e8);
    for (int anew = 0; anew <= 2000; ++anew) {
      for (int from = anew - size_; from <= anew; ++from) {
        if (from < 0)
          continue;
        nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]);
      }
    }
    for (int j = 0; j <= 2000; ++j) {
      ans[j] = max(ans[j], nans[j]);
    }
  }
  int finans;
  for (int i = K; i <= K; ++i) {
    finans = ans[i];
  }
  if (finans > -4e8) {

    cout << finans << endl;
  } else {
    cout << "Impossible" << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

struct Edge {
  int from;
  int to;
  int cost;
};
vector<vector<vector<int>>> *dp;
int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<Edge>> edges(N);
  vector<vector<int>> haits(N);
  /*for (int i = 0; i < M; ++i) {
          int a, b, c; cin >> a >> b >> c;
          a--; b--;
          if (c) {
                  edges[a].push_back(Edge{ a,b,c });
                  edges[b].push_back(Edge{ b,a,c });
          }
          else {
                  haits[a].push_back(b);
                  haits[b].push_back(a);
          }
  }
  vector<vector<int>>hlines;
  int num = -1;
  vector<bool>oks(N);
  for (int i = 0; i < M; ++i) {
          if (haits[i].size() == 1&&!oks[i]) {
                  oks[i] = true;
                  num++;
                  hlines.push_back(vector<int>());
                  int next = haits[i][0];
                  while (1) {
                          hlines[num].push_back(next);
                          int anext = -1;
                          for (auto e : haits[next]) {
                                  if (e != next) {
                                          anext = e;
                                  }
                          }
                          if (anext == -1)break;
                          next = 1;
                  }
          }
  }*/
  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(Edge{a, b, c});
    edges[b].push_back(Edge{b, a, c});
  }
  vector<bool> oks(N);
  vector<vector<int>> line_loves;
  {
    vector<vector<int>> lines;
    int num = -1;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 1 && !oks[i]) {
        oks[i] = true;
        num++;
        lines.push_back(vector<int>());
        line_loves.push_back(vector<int>());
        lines[num].push_back(i);
        line_loves[num].push_back(edges[i][0].cost);
        int next = edges[i][0].to;
        while (1) {
          oks[next] = true;
          int anext = -1;
          for (auto e : edges[next]) {
            if (!oks[e.to]) {
              anext = e.to;
              line_loves[num].push_back(e.cost);
            }
          }
          if (anext == -1)
            break;
          next = anext;
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 0) {
        oks[i] = true;
        num++;
        lines.push_back(vector<int>());
        line_loves.push_back(vector<int>());
        lines[num].push_back(i);
      }
    }
  }

  vector<vector<int>> circle_loves;
  {
    vector<vector<int>> circles;
    int num = -1;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() == 2 && !oks[i]) {
        oks[i] = true;
        num++;
        circles.push_back(vector<int>());
        circle_loves.push_back(vector<int>());
        circles[num].push_back(i);
        circle_loves[num].push_back(edges[i][0].cost);
        int next = edges[i][0].to;
        while (1) {
          oks[next] = true;
          circles[num].push_back(next);
          int anext = -1;
          for (auto e : edges[next]) {
            if (!oks[e.to]) {
              anext = e.to;
              circle_loves[num].push_back(e.cost);
            }
          }
          if (anext == -1 || oks[anext])
            break;
          next = anext;
        }
        if (edges[next][0].to == circles[num][0]) {
          circle_loves[num].push_back(edges[next][0].cost);
        } else {
          circle_loves[num].push_back(edges[next][1].cost);
        }
      }
    }
  }

  vector<int> ans(2001, -9e8);
  ans[0] = 0;
  for (auto l : line_loves) {
    const int size_ = l.size() + 1;
    dp = new vector<vector<vector<int>>>(
        2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
    // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
    // vector<int>(2, -9e8)));
    (*dp)[0][0][0] = 0;
    (*dp)[0][1][1] = 0;
    for (int i = 0; i < size_ - 1; ++i) {
      int tar = i & 1, cur = tar ^ 1;
      for (int k = 0; k < size_ + 1; ++k) {
        for (int l = 0; l < 2; ++l) {
          (*dp)[cur][k][l] = -9e8;
        }
      }
      for (int j = 0; j < size_; ++j) {

        if (i == 0)
          assert(tar == 0 && cur == 1);
        if (l[i]) {
          (*dp)[cur][j][0] =
              max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
          (*dp)[cur][j + 1][1] =
              max((*dp)[cur][j + 1][1],
                  max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
        } else {
          (*dp)[cur][j][0] =
              max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
          (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
        }
      }
    }
    vector<int> nowans(size_ + 1, -9e8);
    for (int j = 0; j <= size_; ++j) {
      nowans[j] =
          max((*dp)[(size_ - 1) & 1][j][0], (*dp)[(size_ - 1) & 1][j][1]);
    }

    vector<int> nans(2001, -9e8);
    for (int anew = 0; anew <= 2000; ++anew) {
      for (int from = anew - size_; from <= anew; ++from) {
        if (from < 0)
          continue;
        nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]);
      }
    }
    for (int j = 0; j <= 2000; ++j) {
      ans[j] = max(ans[j], nans[j]);
    }
    free(dp);
  }
  for (auto l : circle_loves) {

    const int size_ = l.size() + 1;
    vector<int> nowans(size_ + 1, -9e8);
    {

      dp = new vector<vector<vector<int>>>(
          2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
      // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
      // vector<int>(2, -9e8)));
      (*dp)[0][0][0] = 0;
      (*dp)[0][1][1] = -9e8;
      for (int i = 0; i < size_ - 1; ++i) {
        int tar = i & 1, cur = tar ^ 1;
        for (int k = 0; k < size_ + 1; ++k) {
          for (int l = 0; l < 2; ++l) {
            (*dp)[cur][k][l] = -9e8;
          }
        }
        for (int j = 0; j < size_; ++j) {

          if (i == 0)
            assert(tar == 0 && cur == 1);
          if (l[i]) {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] =
                max((*dp)[cur][j + 1][1],
                    max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
          } else {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
          }
        }
      }
      for (int j = 0; j <= size_; ++j) {
        nowans[j] = (*dp)[(size_ - 1) & 1][j][0];
      }
    }
    {

      dp = new vector<vector<vector<int>>>(
          2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8)));
      // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1,
      // vector<int>(2, -9e8)));(*dp)[0][0][0] = -9e8;
      (*dp)[0][1][1] = 0;
      for (int i = 0; i < size_ - 1; ++i) {
        int tar = i & 1, cur = tar ^ 1;
        for (int k = 0; k < size_ + 1; ++k) {
          for (int l = 0; l < 2; ++l) {
            (*dp)[cur][k][l] = -9e8;
          }
        }
        for (int j = 0; j < size_; ++j) {

          if (i == 0)
            assert(tar == 0 && cur == 1);
          if (l[i]) {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] =
                max((*dp)[cur][j + 1][1],
                    max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i]));
          } else {
            (*dp)[cur][j][0] =
                max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1]));
            (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]);
          }
        }
      }
      for (int j = 1; j <= size_; ++j) {
        nowans[j - 1] = max(nowans[j - 1], (*dp)[(size_ - 1) & 1][j][1]);
      }
    }
    vector<int> nans(2001, -9e8);
    for (int anew = 0; anew <= 2000; ++anew) {
      for (int from = anew - size_; from <= anew; ++from) {
        if (from < 0)
          continue;
        nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]);
      }
    }
    for (int j = 0; j <= 2000; ++j) {
      ans[j] = max(ans[j], nans[j]);
    }
  }
  int finans;
  for (int i = K; i <= K; ++i) {
    finans = ans[i];
  }
  if (finans > -4e8) {

    cout << finans << endl;
  } else {
    cout << "Impossible" << endl;
  }
  return 0;
}