Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "HINHTHANG"
- #include <iostream>
- #include <cstdio>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 8e1 + 5;
- constexpr int Inf = 1e9 + 7;
- int n;
- ll S, a[N], b[N];
- int dp[N][N];
- void Read()
- {
- cin >> n >> S;
- ++n;
- for (int i = 1; i <= n; ++i)
- {
- cin >> a[i];
- a[i] += a[i - 1];
- }
- for (int i = 1; i <= n; ++i)
- {
- cin >> b[i];
- b[i] += b[i - 1];
- }
- }
- void Solve()
- {
- fill_n(&dp[0][0], N * N, Inf);
- for (int i = 2; i <= n; ++i)
- for (int j = 2; j <= n; ++j)
- {
- if (a[i] + b[j] <= S)
- dp[i][j] = 1;
- else
- for (int t = i - 1; t; --t)
- for (int h = j - 1; h; --h)
- if ((a[i] - a[t - 1]) + (b[j] - b[h - 1]) <= S) // cost <= S
- dp[i][j] = min(dp[i][j], dp[t][h] + 1);
- }
- cout << (dp[n][n] >= Inf ? -1 : dp[n][n]);
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Add Comment
Please, Sign In to add comment