Problem p02623 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, k;
  vector<int> a, b;
  vector<int> sum_a, sum_b;
  int n_max, m_max;
  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    b.push_back(tmp);
  }

  for (int i = 0; i < n; i++) {
    int tmp2;
    if (i == 0) {
      sum_a.push_back(a[0]);
      n_max = i + 1;
    } else {
      tmp2 = sum_a[i - 1] + a[i];
      if (tmp2 <= k) {
        sum_a.push_back(tmp2);
        n_max = i + 1;
      } else {
        break;
      }
    }
  }
  for (int i = 0; i < m; i++) {
    int tmp2;
    if (i == 0) {
      sum_b.push_back(b[0]);
      m_max = i + 1;
    } else {
      tmp2 = sum_b[i - 1] + b[i];
      if (tmp2 <= k) {
        sum_b.push_back(tmp2);
        m_max = i + 1;
      } else {
        break;
      }
    }
  }
  // cout << n_max << " " << m_max << endl;
  // for (int i = 0; i < n_max; i++)
  // {
  //   cout << sum_a[i] << " ";
  // }
  // cout << endl;
  // for (int j = 0; j < m_max; j++)
  // {
  //   cout << sum_b[j] << " ";
  // }
  // cout << endl;

  int max = 0;
  int sum = 0;

  // pick 0 books from A
  // pick j books from B
  int max_0;
  for (int j = 0; j < m_max; j++) {
    if (sum_b[j] <= k) {
      // cout << sum_b[j] << endl;
      if (max < j + 1) {
        max = j + 1;
      }
    } else {
      break;
    }
  }
  max_0 = max;

  // pick i books from A
  // pick 0 books from B
  for (int i = 0; i < n_max; i++) {
    if (sum_a[i] <= k) {
      // cout << sum_a[i] << endl;
      if (max < i + 1) {
        max = i + 1;
      }
    } else {
      break;
    }
  }

  // pick max_0-1 to 0 books from A
  // increase the number of books from B
  for (int l = max_0 - 1; l >= 0; l--) {
    sum = sum_b[l];
    for (int o = 0; o < n_max; o++) {
      sum = sum_b[l] + sum_a[o];
      if (sum <= k) {
        if (max < l + 1 + o + 1) {
          max = l + 1 + o + 1;
        }
      } else {
        break;
      }
    }
  }

  // pick i books from A
  // pick j books from B
  // for (int i = 0; i < n_max; i++)
  // {
  //   // sum = sum_a[i];
  //   for (int j = 0; j < m_max; j++)
  //   {
  //     sum = sum_a[i] + sum_b[j];
  //     // cout << sum << endl;
  //     if (sum <= k)
  //     {
  //       if (max < i + 1 + j + 1)
  //       {
  //         max = i + 1 + j + 1;
  //       }
  //     }
  //     else
  //     {
  //       break;
  //     }
  //   }
  // }

  cout << max << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, k;
  vector<int> a, b;
  vector<int> sum_a, sum_b;
  int n_max, m_max;
  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    b.push_back(tmp);
  }

  for (int i = 0; i < n; i++) {
    int tmp2;
    if (i == 0) {
      sum_a.push_back(a[0]);
      n_max = i + 1;
    } else {
      tmp2 = sum_a[i - 1] + a[i];
      if (tmp2 <= k) {
        sum_a.push_back(tmp2);
        n_max = i + 1;
      } else {
        break;
      }
    }
  }
  for (int i = 0; i < m; i++) {
    int tmp2;
    if (i == 0) {
      sum_b.push_back(b[0]);
      m_max = i + 1;
    } else {
      tmp2 = sum_b[i - 1] + b[i];
      if (tmp2 <= k) {
        sum_b.push_back(tmp2);
        m_max = i + 1;
      } else {
        break;
      }
    }
  }
  // cout << n_max << " " << m_max << endl;
  // for (int i = 0; i < n_max; i++)
  // {
  //   cout << sum_a[i] << " ";
  // }
  // cout << endl;
  // for (int j = 0; j < m_max; j++)
  // {
  //   cout << sum_b[j] << " ";
  // }
  // cout << endl;

  int max = 0;
  int sum = 0;

  // pick 0 books from A
  // pick j books from B
  int max_0;
  for (int j = 0; j < m_max; j++) {
    if (sum_b[j] <= k) {
      // cout << sum_b[j] << endl;
      if (max < j + 1) {
        max = j + 1;
      }
    } else {
      break;
    }
  }
  max_0 = max;

  // pick i books from A
  // pick 0 books from B
  for (int i = 0; i < n_max; i++) {
    if (sum_a[i] <= k) {
      // cout << sum_a[i] << endl;
      if (max < i + 1) {
        max = i + 1;
      }
    } else {
      break;
    }
  }

  // pick max_0-1 to 0 books from A
  // increase the number of books from B
  for (int l = max_0 - 1; l >= 0; l--) {
    sum = sum_b[l];
    for (int o = max - l - 1; o < n_max; o++) {
      sum = sum_b[l] + sum_a[o];
      if (sum <= k) {
        if (max < l + 1 + o + 1) {
          max = l + 1 + o + 1;
        }
      } else {
        break;
      }
    }
  }

  // pick i books from A
  // pick j books from B
  // for (int i = 0; i < n_max; i++)
  // {
  //   // sum = sum_a[i];
  //   for (int j = 0; j < m_max; j++)
  //   {
  //     sum = sum_a[i] + sum_b[j];
  //     // cout << sum << endl;
  //     if (sum <= k)
  //     {
  //       if (max < i + 1 + j + 1)
  //       {
  //         max = i + 1 + j + 1;
  //       }
  //     }
  //     else
  //     {
  //       break;
  //     }
  //   }
  // }

  cout << max << endl;
  return 0;
}