Problem p00943 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LD long double
#define SC(t, x) static_cast<t>(x)
#define AR(t) vector<t>
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define PIL pair<int, LL>
#define PLI pair<LL, int>
#define PQ priority_queue
#define GT(type) greater<type>
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define PRF first
#define PRS second
#define INIT(ar, val) memset(ar, val, sizeof(ar))
#define lp(loop, start, end) for (int loop = start; loop < end; ++loop)
#define lpd(loop, start, end) for (int loop = start; loop > end; --loop)
#define lpi(loop, start, end) for (int loop = start; loop <= end; ++loop)
#define lpdi(loop, start, end) for (int loop = start; loop >= end; --loop)
#define qmax(a, b) (((a) > (b)) ? (a) : (b))
#define qmin(a, b) (((a) < (b)) ? (a) : (b))
#define qabs(a) (((a) >= 0) ? (a) : (-(a)))

const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 47;
const int MAXM = 4007;

struct eT {
  void setd(int _u, int _v, int _l) { u = _u, v = _v, last = _l; }

  int u, v, last;
} edge[MAXM];

int n, m;
int ans;
int a[MAXN];
int ke, la[MAXN];
int cc[MAXN];

void init();
void input();
void work();

void srch(int now, int na);

int main() {
  init();
  input();
  work();
}

void init() {
  // Init Everything Here

  ios::sync_with_stdio(false);
}

void input() {
  // input method

  scanf("%d%d", &n, &m);
  lpi(i, 1, n) scanf("%d", &a[i]);
  int u, v;
  ke = 0;
  INIT(la, -1);
  lp(i, 0, m) {
    scanf("%d%d", &u, &v);
    edge[ke].setd(u, v, la[u]);
    la[u] = ke++;
    edge[ke].setd(v, u, la[v]);
    la[v] = ke++;
  }
}

void work() {
  // main work

  ans = INF;
  cc[1] = 1;
  srch(1, a[1]);

  printf("%d\\n", ans);
}

void srch(int now, int na) {
  if (na >= ans)
    return;
  if (now == n) {
    for (int i = la[now]; ~i; i = edge[i].last)
      if (!cc[edge[i].v])
        na += a[edge[i].v];
    ans = qmin(ans, na);
    return;
  }

  int ta;
  ta = na;
  for (int j = la[now]; ~j; j = edge[j].last)
    if (!cc[edge[j].v]++)
      ta += a[edge[j].v];

  for (int i = la[now]; ~i; i = edge[i].last) {
    if (cc[edge[i].v] == 1) {
      srch(edge[i].v, ta);
    }
  }

  for (int j = la[now]; ~j; j = edge[j].last)
    --cc[edge[j].v];
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50, inf = 1 << 29;
int n, m, ans = 1 << 29;
vector<int> G[N];
set<ll> s[N];
int a[N], mn[N];
void dfs(int u, int sum, ll mask, ll vis) {
  if (s[u].find(vis) != s[u].end())
    return;
  s[u].insert(vis);
  vis ^= (1ll << u);
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i], t = sum;
    ll S = mask;
    for (int j = 0; j < G[v].size(); ++j) {
      int k = G[v][j];
      if (!(S & (1ll << k))) {
        S |= (1ll << k);
        t += a[k];
      }
    }
    if (t <= mn[v]) {
      mn[v] = t;
      dfs(v, t, S, vis);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  memset(mn, 0x3f3f, sizeof(mn));
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    G[i].clear();
    s[i].clear();
  }
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  G[0].push_back(1);
  dfs(0, 0, 0, 0);
  printf("%d\\n", mn[n]);
  return 0;
}