Skip to content

Instantly share code, notes, and snippets.

@latte0119
Created September 12, 2016 13:59
Show Gist options
  • Save latte0119/6e6ea0901b01c6a9799bf602dd035b7d to your computer and use it in GitHub Desktop.
Save latte0119/6e6ea0901b01c6a9799bf602dd035b7d to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
const int INF=1001001001001001001ll;
int N,M;
int dp[1001][3001];
int v[3000],h[3000];
int t[1000],s[1000];
int accv[3001];
signed main(){
cin>>N>>M;
rep(i,N)cin>>v[i]>>h[i],accv[i+1]=accv[i]+v[i];
rep(i,M)cin>>t[i]>>s[i];
fill_n(*dp,1001*3001,-INF);
dp[0][0]=0;
for(int i=0;i<M;i++){
for(int j=0;j<=N;j++){
if(accv[j]>t[i])break;
if(j){
chmax(dp[i+1][j],dp[i][j-1]+h[j-1]);
}
if(j>1)chmax(dp[i+1][j],dp[i+1][j-1]+h[j-1]+abs(h[j-2]-h[j-1]));
}
for(int j=0;j<=N;j++)chmax(dp[i+1][j],dp[i][j]);
for(int j=0;j<=N;j++){
if(dp[i+1][j]<s[i])dp[i+1][j]=-INF;
}
}
for(int i=0;i<=N;i++){
if(dp[M][i]!=-INF){
cout<<t[M-1]-accv[i]<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment