Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz(x) (int)(x).size()
- #define li long long
- #define ld long double
- #define x first
- #define y second
- #define pt pair<int, int>
- #define pll pair<ll, ll>
- #define forn(i, t) for(int i = 0; i < (t); i++)
- #define fore(i, f, t) for(int i = (f); i < (t); i++)
- #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- using namespace std;
- const int INF = 1e9 + 7;
- const int MOD = 1e9 + 7;
- const li INF64 = 1e18;
- const ld EPS = 1e-7;
- mt19937 myrand(time(NULL));
- const int N = 2000 + 21;
- int n;
- int a[N][5];
- bool read(){
- if(scanf("%d", &n) != 1)
- return 0;
- forn(i, n){
- forn(j, 4)
- scanf("%d", &a[i][j]);
- a[i][4] = (a[i][1] > a[i][2] ? 0 : a[i][1] + a[i][3] > a[i][2] ? (a[i][2] + a[i][3] - a[i][1] + 2) / 2 : INF);
- }
- return 1;
- }
- int dp[N][N];
- void solve(){
- forn(i, n + 1) forn(j, N) dp[i][j] = INF;
- dp[0][0] = 0;
- forn(i, n)
- forn(j, N){
- if (j + a[i][0] < N)
- dp[i + 1][j + a[i][0]] = min(dp[i + 1][j + a[i][0]], dp[i][j] + a[i][4]);
- dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
- }
- int sum = 0, ans = INF;
- forn(i, n)
- sum += a[i][0];
- forn(i, N)
- if (sum - i < i)
- ans = min(ans, dp[n][i]);
- if (ans == INF)
- printf("impossible\n");
- else
- printf("%d\n", ans);
- }
- int main(){
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #endif
- while(read())
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement