Problem p02748 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _DEBUG 1

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define check(s)
#endif

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void solve(ll A, ll B, ll M, std::vector<ll> a, std::vector<ll> b,
           std::vector<ll> x, std::vector<ll> y, std::vector<ll> c) {
  ll result = -1;

  rep(i, M) {
    ll price = a.at(x.at(i) - 1) + b.at(y.at(i) - 1) - c.at(i);
    result = result != -1 ? min(price, result) : price;
  }
  rep(i, A) {
    rep(j, B) {
      ll price = a.at(i) + b.at(j);
      if (price < result) {
        result = price;
      }
    }
  }

  cout << result << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  ll A;
  scanf("%lld", &A);
  ll B;
  scanf("%lld", &B);
  ll M;
  scanf("%lld", &M);
  std::vector<ll> a(A);
  for (int i = 0; i < A; i++) {
    scanf("%lld", &a[i]);
  }
  std::vector<ll> b(B);
  for (int i = 0; i < B; i++) {
    scanf("%lld", &b[i]);
  }
  std::vector<ll> x(M);
  std::vector<ll> y(M);
  std::vector<ll> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &x[i]);
    scanf("%lld", &y[i]);
    scanf("%lld", &c[i]);
  }
  solve(A, B, M, std::move(a), std::move(b), std::move(x), std::move(y),
        std::move(c));
  return 0;
}


Fixed submission:
#define _DEBUG 1

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define check(s)
#endif

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void solve(ll A, ll B, ll M, std::vector<ll> a, std::vector<ll> b,
           std::vector<ll> x, std::vector<ll> y, std::vector<ll> c) {
  ll result = -1;

  rep(i, M) {
    ll price = a.at(x.at(i) - 1) + b.at(y.at(i) - 1) - c.at(i);
    result = result != -1 ? min(price, result) : price;
  }

  ll minA = -1;
  ll minB = -1;
  // a の min + b の min でよさそう
  rep(i, A) { minA = minA != -1 ? min(minA, a.at(i)) : a.at(i); }
  rep(i, B) { minB = minB != -1 ? min(minB, b.at(i)) : b.at(i); }
  result = min(minA + minB, result);

  cout << result << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  ll A;
  scanf("%lld", &A);
  ll B;
  scanf("%lld", &B);
  ll M;
  scanf("%lld", &M);
  std::vector<ll> a(A);
  for (int i = 0; i < A; i++) {
    scanf("%lld", &a[i]);
  }
  std::vector<ll> b(B);
  for (int i = 0; i < B; i++) {
    scanf("%lld", &b[i]);
  }
  std::vector<ll> x(M);
  std::vector<ll> y(M);
  std::vector<ll> c(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &x[i]);
    scanf("%lld", &y[i]);
    scanf("%lld", &c[i]);
  }
  solve(A, B, M, std::move(a), std::move(b), std::move(x), std::move(y),
        std::move(c));
  return 0;
}