alexjercan/bugnet huggingface dataset:
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int min(int n, int m);

int main() {
  int N, M, D[1001], C[1001];
  int ew[1001][1001] = {0};

  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &D[i]);
  }
  for (int i = 1; i <= M; i++) {
    scanf("%d", &C[i]);
  }

  for (int i = 1; i <= M; i++) {
    ew[i][i] = ew[i - 1][i - 1] + C[i] * D[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= M; j++) {
      ew[i][j] = min(ew[i][j - 1], ew[i - 1][j - 1] + D[i] * C[j]);
    }
  }

  printf("%d\\n", ew[N][M]);
  return 0;
}

int min(int n, int m) {
  if (n >= m) {
    return m;
  } else {
    return n;
  }
}

Error: 0

Fixed submission:

#include <stdio.h>

int min(int n, int m);

int main() {
  int N, M, D[1001], C[1001];
  int ew[500][500] = {0};

  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &D[i]);
  }
  for (int i = 1; i <= M; i++) {
    scanf("%d", &C[i]);
  }

  for (int i = 1; i <= M; i++) {
    ew[i][i] = ew[i - 1][i - 1] + C[i] * D[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= M; j++) {
      ew[i][j] = min(ew[i][j - 1], ew[i - 1][j - 1] + D[i] * C[j]);
    }
  }
  printf("%d\\n", ew[N][M]);
  return 0;
}

int min(int n, int m) {
  if (n >= m) {
    return m;
  } else {
    return n;
  }
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
  int n, m, a[1001], b[1001], i, k;

  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  a[0] = 0;
  b[0] = 0;
  for (i = 0; i <= n; i++) {
    for (k = 0; k <= m; k++)
      dp[i][k] = MM;
  }

  dp[0][0] = 0;
  for (i = 0; i < m; i++) {
    for (k = 0; k <= n; k++) {
      if (dp[i + 1][k] > dp[i][k])
        dp[i + 1][k] = dp[i][k];
      if (k + 1 <= n && dp[i][k] != MM)
        dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
    }
  }
  printf("%d\\n", dp[m][n]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
  int n, m, a[1001], b[1001], i, k;

  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  a[0] = 0;
  b[0] = 0;
  for (i = 0; i <= n; i++) {
    for (k = 0; k <= m; k++)
      dp[i][k] = MM;
  }

  dp[0][0] = 0;
  for (i = 0; i <= m; i++) {
    for (k = 0; k <= n; k++) {
      if (dp[i + 1][k] > dp[i][k])
        dp[i + 1][k] = dp[i][k];
      if (k + 1 <= n && dp[i][k] != MM)
        dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
    }
  }
  printf("%d\\n", dp[m][n]);
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

static int D[1000], C[1000], memo[1000][1000];

int f(int i, int j) {
  if (i == -1 && j == -1)
    return 0;
  if (i == -1)
    return 0;
  if (j == -1)
    return 1000000000;
  if (memo[i][j] != 0)
    return memo[i][j];
  return memo[i][j] = ((f(i - 1, j - 1) + D[i] * C[j]) < f(i, j - 1)
                           ? (f(i - 1, j - 1) + D[i] * C[j])
                           : f(i, j - 1));
}

int main() {
  int N, M, i, j, k, l, score = 0, dmax = 0, min = 1000000000;
  static int Min[1000];
  scanf("%d%d", &N, &M);
  for (i = 0; i < N; i++)
    scanf("%d", &D[i]);
  for (i = 0; i < M; i++)
    scanf("%d", &C[i]);
  for (i = 0; N + i - 1 <= M - 1; i++)
    Min[i] = f(N - 1, N + i - 1);
  for (i = 0; N + i - 1 <= M - 1; i++)
    if (min > Min[i])
      min = Min[i];
  printf("%d\\n", min);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

static int D[1000], C[1000];

int f(int i, int j) {
  if (i == -1 && j == -1)
    return 0;
  if (i == -1)
    return 0;
  if (j == -1)
    return 1000000;
  return ((f(i - 1, j - 1) + D[i] * C[j]) < f(i, j - 1)
              ? (f(i - 1, j - 1) + D[i] * C[j])
              : f(i, j - 1));
}

int main() {
  int N, M, i, j, k, l, score = 0, dmax = 0, min = 1000000;
  static int Min[1000];
  scanf("%d%d", &N, &M);
  for (i = 0; i < N; i++)
    scanf("%d", &D[i]);
  for (i = 0; i < M; i++)
    scanf("%d", &C[i]);
  for (i = 0; N + i - 1 <= M - 1; i++)
    Min[i] = f(N - 1, N + i - 1);
  for (i = 0; N + i - 1 <= M - 1; i++)
    if (min > Min[i])
      min = Min[i];
  printf("%d\\n", min);

  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, m;
int d[1001];
int c[1001];
int dp[1002][1001];
int dp_hiroudo(int i, int j) {
  if (i == n) {
    return 0;
  } else if (dp[i][j] == -1) {
    dp[i][j] =
        min(dp_hiroudo(i + 1, j + 1) + d[i] * c[j], dp_hiroudo(i, j + 1));
  }
  return dp[i][j];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  for (int i = 0; i < 1002; i++) {
    for (int j = 0; j < 1001; j++) {
      if (n - i > m - j)
        dp[i][j] = 1000000000;
      else
        dp[i][j] = -1;
    }
  }
  dp_hiroudo(0, 0);
  cout << dp[0][0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n, m;
int d[1001];
int c[1001];
int dp[1001][1000];
int dp_hiroudo(int i, int j) {
  if (i == n) {
    return 0;
  } else if (dp[i][j] == -1) {
    dp[i][j] =
        min(dp_hiroudo(i + 1, j + 1) + d[i] * c[j], dp_hiroudo(i, j + 1));
  }
  return dp[i][j];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 1000; j++) {
      if (n - i > m - j)
        dp[i][j] = 1000000000;
      else
        dp[i][j] = -1;
    }
  }
  dp_hiroudo(0, 0);
  cout << dp[0][0] << endl;
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);

int main() {
  int n, m;
  int d[1001], c[1001];
  int dp[1001][1001];

  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 1; i < m + 1; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[i - 1][j] == INF)
        continue;
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[j] * c[i - 1]);
    }
  }

  int ans = INF;
  for (int i = 0; i < m + 1; i++) {
    ans = min(ans, dp[i][n]);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 25);

int main() {
  int n, m;
  int d[101], c[101];
  int dp[1001][1001];

  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 1; i < m + 1; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[i - 1][j] == INF)
        continue;
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[j] * c[i - 1]);
    }
  }

  int ans = INF;
  for (int i = 0; i < m + 1; i++) {
    ans = min(ans, dp[i][n]);
  }

  cout << ans << endl;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>

const int INF = 1000 * 1000 * 1000 + 1000;
int N, M, D[1123], C[1123];
int memo[1123][1123], ans = 0;

// dp(i,j) == ??????i+1?????§j+1??\\??\\???????????£?????????????°?????????´???
// dp(0,????????§???) == 0
// dp(i,j) == min( dp(i-1,j-1)+D[i]*C[j],dp(i,j-1))

int dp(int i, int j) {
  if (i > j) {
    return INF;
  }
  if (i == -1) {
    return 0;
  } else {
    if (memo[i][j] != -1) {
      return memo[i][j];
    }
    return memo[i][j] = std::min(dp(i - 1, j - 1) + D[i] * C[j], dp(i, j - 1));
  }
}

int main(void) {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &D[i]);
  }
  for (int i = 0; i < M; ++i) {
    scanf("%d", &C[i]);
  }

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      memo[i][j] = -1;
    }
  }

  printf("%d\\n", dp(N - 1, M - 1));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

const int INF = 1000 * 1000 * 1000 + 1000;
int N, M, D[1123], C[1123];
int ans = 0;

// dp(i,j) == ??????i+1?????§j+1??\\??\\???????????£?????????????°?????????´???
// dp(0,????????§???) == 0
// dp(i,j) == min( dp(i-1,j-1)+D[i]*C[j],dp(i,j-1))

int dp(int i, int j) {
  if (i > j) {
    return INF;
  }
  if (i == -1) {
    return 0;
  } else {
    return std::min(dp(i - 1, j - 1) + D[i] * C[j], dp(i, j - 1));
  }
}

int main(void) {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &D[i]);
  }
  for (int i = 0; i < M; ++i) {
    scanf("%d", &C[i]);
  }

  printf("%d\\n", dp(N - 1, M - 1));
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
  int N, M;
  int C[1001], D[1001];
  int i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> D[i];
  }
  for (i = 1; i <= M; i++) {
    cin >> C[i];
  }
  for (i = 1; i <= M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i][j] = MAX;
    }
  }

  dp[1][0] = 0;
  dp[1][1] = D[1] * C[1];
  for (i = 1; i < M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j < N) {
        dp[i + 1][j + 1] =
            min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
      }
    }
  }
  /*for(i=1;i<=M;i++){
          for(j=0;j<=N;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }*/

  cout << dp[M][N] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
  int N, M;
  int C[1001], D[1001];
  int i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> D[i];
  }
  for (i = 1; i <= M; i++) {
    cin >> C[i];
  }
  for (i = 1; i <= M; i++) {
    for (j = 0; j <= N; j++) {
      dp[i][j] = MAX;
    }
  }

  dp[1][0] = 0;
  dp[1][1] = D[1] * C[1];
  for (i = 1; i <= M + 1; i++) {
    for (j = 0; j <= N; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j < N) {
        dp[i + 1][j + 1] =
            min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
      }
    }
  }
  /*for(i=1;i<=M;i++){
          for(j=0;j<=N;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }*/

  cout << dp[M][N] << endl;
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int N, M;
  int h[1500][1500] = {0}, l[1500] = {0}, t[1500] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  for (int j = 0; j < M; j++) {
    cin >> t[j];
  }
  for (int j = 0; j < N; j++) {
    for (int i = j; i < j + M - N + 1; i++) {
      if (j == 0) {
        h[j][i] = l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != 0) {
          h[j][i] = h[j][i - 1];
        }
      } else {
        h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != j) {
          h[j][i] = h[j][i - 1];
        }
      }
    }
  }
  cout << h[N - 1][M - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int N, M;
  int h[100][100] = {0}, l[1500] = {0}, t[1500] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  for (int j = 0; j < M; j++) {
    cin >> t[j];
  }
  for (int j = 0; j < N; j++) {
    for (int i = j; i < j + M - N + 1; i++) {
      if (j == 0) {
        h[j][i] = l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != 0) {
          h[j][i] = h[j][i - 1];
        }
      } else {
        h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
        if (h[j][i] >= h[j][i - 1] && i != j) {
          h[j][i] = h[j][i - 1];
        }
      }
    }
  }
  cout << h[N - 1][M - 1] << endl;
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
#define INF INT_MAX
int n, m;
vector<int> d;
vector<int> c;
vector<vector<int>> dp;

void solve(int nisu, int shi) {
  if (nisu > 0)
    dp[shi][nisu] = min(dp[shi][nisu], dp[shi][nisu - 1]);
  if (nisu < m && shi < n) {
    dp[shi][nisu + 1] = min(dp[shi][nisu + 1], dp[shi][nisu]);
    dp[shi + 1][nisu + 1] =
        min(dp[shi + 1][nisu + 1], dp[shi][nisu] + c[nisu] * d[shi]);
    solve(nisu + 1, shi);
    if (dp[shi + 1][nisu] == INF)
      solve(nisu + 1, shi + 1);
  }
}

int main() {
  cin >> n >> m;
  d.resize(n);
  c.resize(m);
  dp.resize(n + 1);
  for (int i = 0; i <= n; i++) {
    dp[i].resize(m + 1);
    for (int j = 0; j <= m; j++)
      dp[i][j] = INF;
  }
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  dp[0][0] = 0;
  solve(0, 0);
  int ans = INF;
  for (int i = 0; i <= m; i++)
    ans = min(ans, dp[n][i]);

  // for(int i=0;i<=m;i++){
  // for(int j=0;j<=n;j++) cout << dp[j][i]<<" ";
  // cout<< endl;
  // }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
#define INF INT_MAX
int n, m;
vector<int> d;
vector<int> c;
vector<vector<int>> dp;

void solve(int nisu, int shi) {
  if (nisu > 0)
    dp[shi][nisu] = min(dp[shi][nisu], dp[shi][nisu - 1]);
  if (nisu < m && shi < n) {
    dp[shi][nisu + 1] = min(dp[shi][nisu + 1], dp[shi][nisu]);
    dp[shi + 1][nisu + 1] =
        min(dp[shi + 1][nisu + 1], dp[shi][nisu] + c[nisu] * d[shi]);
    solve(nisu + 1, shi);
    solve(nisu + 1, shi + 1);
  }
}

int main() {
  cin >> n >> m;
  d.resize(n);
  c.resize(m);
  dp.resize(n + 1);
  for (int i = 0; i <= n; i++) {
    dp[i].resize(m + 1);
    for (int j = 0; j <= m; j++)
      dp[i][j] = INF;
  }
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  dp[0][0] = 0;
  solve(0, 0);
  // int ans = INF;
  // for(int i=0;i<=m;i++) ans=min(ans,dp[n][i]);

  // for(int i=0;i<=m;i++){
  //   for(int j=0;j<=n;j++) cout << dp[j][i]<<" ";
  //   cout<< endl;
  //  }
  cout << dp[n][m] << endl;

  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {

  int n, m;
  cin >> n >> m;
  long long dp[1010][1010];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = INF;
    }
  }
  int d[1010];
  for (int j = 1; j <= n; j++) {
    cin >> d[j];
  }

  int c[1010];
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }

  for (int i = 0; i <= m; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + c[i] * d[j]);
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main(void) {

  int n, m;
  cin >> n >> m;
  int dp[101][101];
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = INF;
    }
  }
  int d[1010];
  for (int j = 1; j <= n; j++) {
    cin >> d[j];
  }

  int c[1010];
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }

  for (int i = 0; i <= m; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + c[i] * d[j]);
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
long long dp[1001][1001];
long long e[1001][1001];
long long ans[1001];
long long n, m, d[1001], c[1001];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < m - (n - 1); i++) {
    dp[0][i] = d[0] * c[i];
  }
  // cout<<" ++++++"<<endl;
  for (int i = 1; i < n; i++) {
    for (int j = i; j < m - (n - i) + 1; j++) {
      // cout<<"====="<<endl;
      for (int z = i - 1; z < j; z++) {
        // cout<<"QQQQQQQ"<<endl;
        ans[z - i + 1] = dp[i - 1][z];
      }
      sort(ans, ans + j - (i - 1));
      dp[i][j] = ans[0] + d[i] * c[j];
    }
  }
  for (int i = n - 1; i < m; i++) {
    ans[i - (n - 1)] = dp[n - 1][i];
  }
  sort(ans, ans + (m - n) + 1);
  cout << ans[0] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
struct ST {
  int ni;
  long long ans;
  int k;
  int y;
};
using namespace std;
int main() {
  int n, m, c[1001], d[1001];
  stack<ST> S;
  ST a;
  ST b;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  a.ni = 0;
  a.ans = 0;
  a.y = 0;
  a.k = m;
  S.push(a);
  int min = 10000000;
  while (!S.empty()) {
    b = S.top();
    S.pop();
    // cout<<"**"<<endl;
    if (b.ni == n) {
      // cout<<"******"<<b.ans<<endl;
      if (min > b.ans) {
        min = b.ans;
      }
    } else {
      if (b.y < m && b.k >= 0 && b.ans < min && n - b.ni <= m - b.y) {
        // cout<<"*****"<<endl;
        a.ans = b.ans + d[b.ni] * c[b.y]; // cout<<b.ni<<"  "<<b.y<<endl;
        a.ni = b.ni + 1;
        a.y = b.y + 1;
        a.k = b.k - 1;
        // cout<<a.ans<<"AA"<<a.ni<<"AA"<<a.y<<"AA"<<a.k<<"AA"<<endl;
        S.push(a);
        b.k--;
        b.y++;
        // cout<<b.ans<<"AA"<<b.ni<<"AA"<<b.y<<"AA"<<b.k<<"AA"<<endl;
        S.push(b);
      }
    }
  }
  cout << min << endl;
}
Problem p00534 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAX_N = 1000;
const int MAX_M = 1000;
const int INF = 1000000000;
int dp[MAX_M][MAX_N];
int dis[MAX_N];
int wea[MAX_M];
int n, m;
int rec(int i, int j) {
  if (j == n) {
    return 0;
  }
  if (i == m) {
    return INF;
  }
  if (dp[i][j] >= 0) {
    return dp[i][j];
  }

  return dp[i][j] = min(rec(i + 1, j), rec(i + 1, j + 1) + wea[i] * dis[j]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &dis[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &wea[i]);
  }
  memset(dp, -1, sizeof(dp));

  printf("%d\\n", rec(0, 0));
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAX_N = 1000;
const int MAX_M = 1000;
const int INF = 100000000;
int dp[MAX_M][MAX_N];
int dis[MAX_N];
int wea[MAX_M];
int n, m;
int rec(int i, int j) {
  if (dp[i][j] >= 0) {
    return dp[i][j];
  }

  if (j == n) {
    return 0;
  }
  if (i == m) {
    return INF;
  }

  return dp[i][j] = min(rec(i + 1, j), rec(i + 1, j + 1) + wea[i] * dis[j]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &dis[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &wea[i]);
  }
  memset(dp, -1, sizeof(dp));

  printf("%d\\n", rec(0, 0));
  return 0;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits //stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int d[1000], c[1000];
int memo[1000][1000];
int n, m;
int solve(int i, int j) {
  if (i == n) {
    return 0;
  } else if (n - i == m - j) {
    if (memo[i + 1][j + 1] == -1)
      memo[i + 1][j + 1] = solve(i + 1, j + 1);
    return c[j] * d[i] + memo[i + 1][j + 1];
  } else {
    int res1, res2;
    if (memo[i][j + 1] != -1) {
      res1 = memo[i][j + 1];
    } else
      res1 = memo[i][j + 1] = solve(i, j + 1);
    if (memo[i + 1][j + 1] != -1) {
      res2 = d[i] * c[j] + memo[i + 1][j + 1];
    } else {
      res2 = memo[i + 1][j + 1] = solve(i + 1, j + 1);
      res2 += (d[i] * c[j]);
    }
    return min(res1, res2);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
    for (int j = 0; j < m; j++) {
      memo[i][j] = -1;
    }
  }
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int ans = solve(0, 0);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits //stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int d[1000], c[1000];
int solve(int i, int j, int n, int m, int sum) {
  if (i == n) {
    return sum;
  } else if (n - i == m - j) {
    return solve(i + 1, j + 1, n, m, sum + d[i] * c[j]);
  } else {
    return min(solve(i, j + 1, n, m, sum),
               solve(i + 1, j + 1, n, m, sum + d[i] * c[j]));
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int ans = solve(0, 0, n, m, 0);
  cout << ans << endl;
}
Problem p00534 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int n, m, day[1010] = {0}, cap[1010] = {0}, dp[1010][1010] = {0};
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &cap[i]);
  for (int i = 0; i < m; i++)
    scanf("%d", &day[i]);
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      if (j == 0)
        dp[j][i] = 0;
      else if (i < j || i == 0)
        dp[j][i] = -1;
      else if (dp[j][i - 1] == -1)
        dp[j][i] = dp[j - 1][i - 1] + (cap[j - 1] * day[i - 1]);
      else
        dp[j][i] =
            min(dp[j - 1][i - 1] + cap[j - 1] * day[i - 1], dp[j][i - 1]);
    }
  }
  /*for(int i = 0; i < m + 1; i++)
  {
      for(int j = 0; j < n + 1; j++)
      {
          printf("%d ",dp[j][i]);
      }
      printf("\\n");
  }*/
  printf("%d\\n", dp[n][m]);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int n, time;
int longth[2000] = {0}, wea[2000] = {0};
int mem = 1000000000;
int ex(int tosi, int now, int sum) {
  if (time < now)
    return 0;
  if (now > time)
    return 0;
  if (mem < sum)
    return 0;
  if (n == tosi - 1) {
    if (mem > sum)
      mem = sum;
    return 0;
  }
  ex(tosi + 1, now + 1, (longth[tosi] * wea[now + 1]) + sum);
  ex(tosi, now + 1, sum);
  return 0;
}
int main() {
  scanf("%d%d", &n, &time);
  for (int i = 1; i < n + 1; i++) {
    scanf("%d", &longth[i]);
  }
  for (int i = 1; i < time + 1; i++) {
    scanf("%d", &wea[i]);
  }
  ex(1, 0, 0);
  printf("%d\\n", mem); // next tosi ,, time ,,   ti
  return 0;
}