Problem:
New Year Festival
The
ICPC (Incredibly Colossal and Particularly Comfortable) Theater
is giving a number of traditional events to celebrate the New Year!
Each of the events has its own unalterable duration. Start times of the events are flexible as long as no two events overlap. An event may start immediately after another event ends.
Start times of the events influence the costs. The cost of an event is given by a continuous piecewise linear function (a polygonal line function) of its start time. Different events may have different cost functions.
You are asked to schedule all the events minimizing the total cost.
Input
The input consists of a single test case of the following format.
$n$
$Description_1$
.
.
.
$Description_n$
The first line contains an integer $n$, representing the number of events. $2 \leq n \leq 11$ holds. The descriptions of the events follow. The description of the $i$-th event, $Description_i$ ($1 \leq i \leq n$), has the following format.
$m$ $l$
$x_1$ $y_1$
.
.
.
$x_m$ $y_m$
The integer $m$ in the first line is the number of vertices of the cost function of the event. The integer $l$ in the same line is the duration of the event. $1 \leq m \leq 60$ and $1 \leq l \leq 10^8$ hold.
The following $m$ lines describe the cost function. The $j$-th line of the $m$ lines consists of the two integers $x_j$ and $y_j$ specifying the $j$-th vertex of the cost function. $0 \leq x_1 < ... < x_m \leq 10^8$ and $0 \leq y_j \leq 10^8$ hold. In addition, $(y_{j+1} - y_j )/(x_{j+1} - x_j )$ is an integer for any $1 \leq j < m$.
The start time $t$ of the event must satisfy $x_1 \leq t \leq x_m$. For $j$ ($1 \leq j < m$) satisfying $x_j \leq t \leq x_{j+1}$, the cost of the event is given as $y_j + (t − x_j ) \times (y_{j+1} − y_j )/(x_{j+1} − x_j )$.
The total number of the vertices of all the cost functions is not greater than 60.
Output
Output the minimum possible total cost of the events in a line.
It is guaranteed that there is at least one possible schedule containing no overlap. It can be proved that the answer is an integer.
Sample Input 1
3
3 50
300 2500
350 0
400 3000
2 120
380 0
400 2400
4 160
0 800
400 0
450 100
950 4600
Sample Output 1
1460
Sample Input 2
4
2 160
384 0
1000 2464
3 280
0 2646
441 0
1000 2795
1 160
544 0
2 240
720 0
1220 2000
Sample Output 2
2022
For Sample Input 1, making the (start time, end time) pairs of the three events to be $(330, 380)$, $(380, 500)$, and $(170, 330)$, respectively, achieves the minimum total cost without event overlaps. The cost of the event $1$ is $2500 + (330 − 300) \times (0 − 2500)/(350 − 300) = 1000$. Similarly, the costs of the events $2$ and $3$ are $0$ and $460$, respectively.
For Sample Input 2, the minimum cost is achieved by $(384, 544)$, $(104, 384)$, $(544, 704)$, and $(720, 960)$ for the four events.
Figure K.1. Cost functions in Sample Input 1 and a sample schedule
Figure K.2. Cost functions in Sample Input 2 and a sample schedule
In Figures K.1 and K.2, polylines in the top f
...(truncated)

Submitted solutions:

code_snippet: #ifdef NACHIA
#define _GLIBCXX_DEBUG
#endif

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
using ll = long long;
template<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }
template<class A, class B> void chmin(A& a, const B& b){ if(b < a) a = b; }
using PLL = pair<ll, ll>;
#define rep(i,n) for(ll i=0; i<ll(n); i++)

const ll INF = 1001001001001001001;

struct Event {
    vector<ll> X;
    vector<ll> Y;
    vector<ll> sl;
    ll eval(ll x) const {
        if(X.back() < x || x < X.front()) return INF;
        ll p = upper_bound(X.begin(), X.end(), x) - X.begin() - 1;
        return Y[p] + sl[p] * (x - X[p]);
    }
};

void testcase(){
    ll N; cin >> N;
    vector<ll> L(N);
    vector<Event> E(N);
    rep(i,N){
        ll m; cin >> m >> L[i];
        auto& e = E[i];
        e.X.resize(m);
        e.Y.resize(m);
        e.sl.resize(m);
        rep(j,m) cin >> e.X[j] >> e.Y[j];
        rep(j,m-1) e.sl[j] = (e.Y[j+1] - e.Y[j]) / (e.X[j+1] - e.X[j]);
    }
    //cout << "## input" << endl;
    vector<ll> len(1<<N);
    rep(i,N) rep(j,1<<i) len[j+(1<<i)] = len[j] + L[i];
    struct Wing {
        ll mask;
        ll term;
        ll cost;
        ll isr;
    };
    //cout << "## calc len" << endl;
    vector<pair<ll, Wing>> ql;
    rep(i,N) rep(j,E[i].X.size()){
        ll t = E[i].X[j] + L[i];
        vector<ll> F(1<<N, INF);
        F[1<<i] = E[i].eval(t-L[i]);
        rep(a,1<<N) if(a&(1<<i)){
            ql.push_back({ t - len[a], { a, 60 * i + j, F[a], 0 } });
            rep(b,N) if(!(a&(1<<b))){
                chmin(F[a|(1<<b)], F[a] + E[b].eval(t - len[a|(1<<b)]));
            }
        }
        //for(auto f : F){ cout << f << " "; } cout << endl;
    }
    //cout << "## calc in wings" << endl;
    rep(i,N) rep(j,E[i].X.size()){
        ll t = E[i].X[j] + L[i];
        vector<ll> F(1<<N, INF);
        F[0] = 0;
        rep(a,1<<N) if(!(a&(1<<i))){
            ql.push_back({ t + len[a], { a, 60 * i + j, F[a], 1 } });
            rep(b,N) if(!(a&(1<<b))){
                chmin(F[a|(1<<b)], F[a] + E[b].eval(t + len[a]));
            }
        }
        //for(auto f : F){ cout << f << " "; } cout << endl;
    }
    //cout << "## calc out wings" << endl;
    sort(ql.begin(), ql.end(), [&](const auto& l, const auto& r){ return l.first < r.first; });
    vector<vector<ll>> dpterm(N*61, vector<ll>(1<<N, INF));
    vector<ll> dp(1<<N, INF);
    dp[0] = 0;
    for(auto [t, w] : ql){
        ll xm = (1 << N) - 1 - w.mask;
        if(w.isr){
            for(ll m=xm; ; m=(m-1)&xm){
                chmin(dp[m|w.mask], dpterm[w.term][m] + w.cost);
                if(m == 0) break;
            }
        } else {
            for(ll m=xm; ; m=(m-1)&xm){
                chmin(dpterm[w.term][m|w.mask], dp[m] + w.cost);
                if(m == 0) break;
            }
        }
    }
    cout << dp[(1<<N)-1] << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef NACHIA
    ll T; cin >> T; rep(t,T){
    #endif
    testcase();
    
    #ifdef NACHIA
    cout << "---------------------" << endl; }
    #endif
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 18960
final_rank: 2

code_snippet: #ifdef NACHIA
#define _GLIBCXX_DEBUG
#endif

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
using ll = long long;
template<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }
template<class A, class B> void chmin(A& a, const B& b){ if(b < a) a = b; }
using PLL = pair<ll, ll>;
#define rep(i,n) for(ll i=0; i<ll(n); i++)

const ll INF = 1001001001001001001;

struct Event {
    vector<ll> X;
    vector<ll> Y;
    vector<ll> sl;
    ll eval(ll x) const {
        if(X.back() < x || x < X.front()) return INF;
        ll p = upper_bound(X.begin(), X.end(), x) - X.begin() - 1;
        return Y[p] + sl[p] * (x - X[p]);
    }
};

void testcase(){
    ll N; cin >> N;
    vector<ll> L(N);
    vector<Event> E(N);
    rep(i,N){
        ll m; cin >> m >> L[i];
        auto& e = E[i];
        e.X.resize(m);
        e.Y.resize(m);
        e.sl.resize(m);
        rep(j,m) cin >> e.X[j] >> e.Y[j];
        rep(j,m-1) e.sl[j] = (e.Y[j+1] - e.Y[j]) / (e.X[j+1] - e.X[j]);
    }
    //cout << "## input" << endl;
    vector<ll> len(1<<N);
    rep(i,N) rep(j,1<<i) len[j+(1<<i)] = len[j] + L[i];
    struct Wing {
        ll mask;
        ll term;
        ll cost;
        ll isr;
    };
    //cout << "## calc len" << endl;
    vector<pair<ll, Wing>> ql;
    rep(i,N) rep(j,E[i].X.size()){
        ll t = E[i].X[j] + L[i];
        vector<ll> F(1<<N, INF);
        F[1<<i] = E[i].eval(t-L[i]);
        rep(a,1<<N) if(a&(1<<i)){
            ql.push_back({ t - len[a], { a, 60 * i + j, F[a], 0 } });
            rep(b,N) if(!(a&(1<<b))){
                chmin(F[a|(1<<b)], F[a] + E[b].eval(t - len[a|(1<<b)]));
            }
        }
        //for(auto f : F){ cout << f << " "; } cout << endl;
    }
    //cout << "## calc in wings" << endl;
    rep(i,N) rep(j,E[i].X.size()){
        ll t = E[i].X[j] + L[i];
        vector<ll> F(1<<N, INF);
        F[0] = 0;
        rep(a,1<<N) if(!(a&(1<<i))){
            ql.push_back({ t + len[a], { a, 60 * i + j, F[a], 1 } });
            rep(b,N) if(!(a&(1<<b))){
                chmin(F[a|(1<<b)], F[a] + E[b].eval(t + len[a]));
            }
        }
        //for(auto f : F){ cout << f << " "; } cout << endl;
    }
    //cout << "## calc out wings" << endl;
    sort(ql.begin(), ql.end(), [&](const auto& l, const auto& r){ return l.first < r.first; });
    vector<vector<ll>> dpterm(N*61, vector<ll>(1<<N, INF));
    vector<ll> dp(1<<N, INF);
    dp[0] = 0;
    for(auto [t, w] : ql){
        ll xm = (1 << N) - 1 - w.mask;
        if(w.isr){
            for(ll m=xm; ; m=(m-1)&xm){
                chmin(dp[m|w.mask], dpterm[w.term][m] + w.cost);
                if(m == 0) break;
            }
        } else {
            for(ll m=xm; ; m=(m-1)&xm){
                chmin(dpterm[w.term][m|w.mask], dp[m] + w.cost);
                if(m == 0) break;
            }
        }
    }
    cout << dp[(1<<N)-1] << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef NACHIA
    ll T; cin >> T; rep(t,T){
    #endif
    testcase();
    
    #ifdef NACHIA
    cout << "---------------------" << endl; }
    #endif
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 19028
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL dp4[70][70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    if(v[i].size()==1)return v[i].back().y;
    for(int j=0;j<v[i].size()-1;j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    assert(0);
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;

    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP4(int l,int r,int y){
    if(dp4[l][r][y]!=-1)return dp4[l][r][y];
    dp4[l][r][y]=1e18;
    //LL t=ti[x]-sum[y];
    if(ti[r]-ti[l]<sum[y])return 1e18;
    for(int a=y;;a=(a-1)&y){
        dp4[l][r][y]=min(dp4[l][r][y],DP3(r,a)+DP2(l,y-a));
        if(a==0)break;
    }
    return dp4[l][r][y];
}
LL DP(int x,int y){
    if(x==0)return 0;
    if(y<=0)return 1e18;
    if(dp[x][y]!=-1)return dp[x][y];
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
              //  if(k!=0){
                
  //  printf("%d %d %d %d %lld %lld %lld\n",x,y,k,j,dp[x][y],DP(x-j,k-1),DP4(y,j));
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
               // }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP4(k,y,j),dp[x][y]);
            }
        }
    }
   // printf("%d %d %lld\n",x,y,dp[x][y]);
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %lld",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    MEMS(dp4);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    assert(ans<=2e9);
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
11
1 1
0 100000000
1 1
1 100000000
1 1
2 100000000
1 1
3 100000000
1 1
4 100000000
1 1
5 100000000
1 1 
6 100000000
1 1 
7 100000000
1 1 
8 100000000
1 1 
9 100000000
1 1 
10 100000000

3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 1
time_ms: 1630
memory_kb: 85392
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    if(v[i].size()==1)return v[i].back().y;
    for(int j=0;j<v[i].size()-1;j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    assert(0);
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(x==0)return 0;
    if(y<=0)return 1e18;
    if(dp[x][y]!=-1)return dp[x][y];
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
              //  if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
               // }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %lld",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    assert(ans<=2e9);
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
11
1 1
0 100000000
1 1
1 100000000
1 1
2 100000000
1 1
3 100000000
1 1
4 100000000
1 1
5 100000000
1 1 
6 100000000
1 1 
7 100000000
1 1 
8 100000000
1 1 
9 100000000
1 1 
10 100000000

3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 300
memory_kb: 6916
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    if(v[i].size()==1)return v[i].back().y;
    for(int j=0;j<v[i].size()-1;j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    assert(0);
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(x==0)return 0;
    if(y<=0)return 1e18;
    if(dp[x][y]!=-1)return dp[x][y];
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
              //  if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
               // }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %lld",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
11
1 1
0 100000000
1 1
1 100000000
1 1
2 100000000
1 1
3 100000000
1 1
4 100000000
1 1
5 100000000
1 1 
6 100000000
1 1 
7 100000000
1 1 
8 100000000
1 1 
9 100000000
1 1 
10 100000000

3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 300
memory_kb: 6928
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    if(v[i].size()==1)return v[i].back().y;
    for(int j=0;j<v[i].size()-1;j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(x==0)return 0;
    if(y<=0)return 1e18;
    if(dp[x][y]!=-1)return dp[x][y];
 //   if(x==0)return 0;
 //   if(y==0)
  //  dp[x][y]=1e18;
  //  else
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
              //  if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
               // }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %lld",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 300
memory_kb: 6856
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    if(v[i].size()==1)return v[i].back().y;
    for(int j=0;j<v[i].size()-1;j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(dp[x][y]!=-1)return dp[x][y];
    if(x==0)return 0;
    if(y==0)
    dp[x][y]=1e18;
    else
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
                if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
                }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %lld",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 290
memory_kb: 6872
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
LL l[11];
vector<pll> v[15];
vector<LL> ti;
int n;
LL getc(int i,LL x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    for(int j=0;j<v[i].size();j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            LL t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(dp[x][y]!=-1)return dp[x][y];
    if(x==0)return 0;
    if(y==0)
    dp[x][y]=1e18;
    else
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
                if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
                }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %d",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 280
memory_kb: 6856
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define y second
#define mp make_pair
#define pii pair<int,int>
#define pll pair<LL,LL>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define LL long long 
#define x first
#define pb push_back
#define pi acosl(-1)
#define sqr(x) ((x)*(x))
const int mod=998244353;
LL dp[1<<11][70];
LL dp2[70][1<<11];
LL dp3[70][1<<11];
LL sum[1<<11];
int l[11];
vector<pii> v[15];
vector<int> ti;
int n;
LL getc(int i,int x){
    if(v[i][0].x>x||v[i].back().x<x)return 1e18;
    for(int j=0;j<v[i].size();j++){
        if(v[i][j+1].x>=x){
            return v[i][j].y+(x-v[i][j].x)*((v[i][j+1].y-v[i][j].y)/(v[i][j+1].x-v[i][j].x));
        }
    }
    return v[i].back().y;
}
LL DP2(int x,int y){
    if(dp2[x][y]!=-1)return dp2[x][y];
    if(y==0)return 0;
    dp2[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            int t=ti[x]+sum[y-(1<<i)];
            dp2[x][y]=min(DP2(x,y-(1<<i))+getc(i,t),dp2[x][y]);
        }
    }
    //printf("%d %d %lld\n",x,y,dp2[x][y]);
    return dp2[x][y];
}
LL DP3(int x,int y){
    if(dp3[x][y]!=-1)return dp3[x][y];
    if(y==0)return 0;
    dp3[x][y]=1e18;
    for(int i = 0;i<n;i++){
        if(y&(1<<i)){
            int t=ti[x]-sum[y];
            dp3[x][y]=min(DP3(x,y-(1<<i))+getc(i,t),dp3[x][y]);
        }
    }
    return dp3[x][y];
}
LL DP(int x,int y){
    if(dp[x][y]!=-1)return dp[x][y];
    if(x==0)return 0;
    if(y==0)
    dp[x][y]=1e18;
    else
    dp[x][y]=DP(x,y-1);
    for(int j = x;j>0;j=(j-1)&x){
        LL xx=ti[y]-sum[j];
        for(int k = 0;k<ti.size();k++){
            if(ti[k]>xx){
                if(k!=0){
                    dp[x][y]=min(DP(x-j,k-1)+DP3(y,j),dp[x][y]);
                }
                break;
            }
            else{
                
                dp[x][y]=min(DP(x-j,k)+DP2(k,j),dp[x][y]);
            }
        }
    }
    return dp[x][y];
}
void solve(int T){
    //int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int m;
        scanf("%d %d",&m,&l[i]);
        for(int j=0;j<m;j++){
            int x,y;
            scanf("%d %d",&x,&y);
            v[i].pb(mp(x,y));
            ti.pb(x);
        }
    }
    sort(ti.begin(),ti.end());
    ti.resize(unique(ti.begin(),ti.end())-ti.begin());
    ti.pb(2e9);
    sum[0]=0;
    for(int i = 1;i<(1<<n);i++){
        int x=(i&-i);
        sum[i]=sum[i-x];
        sum[i]+=l[__builtin_popcount(x-1)];
    }
    LL ans=1e18;
    MEMS(dp);
    MEMS(dp2);
    MEMS(dp3);
    ans=min(ans,DP((1<<n)-1,ti.size()-1));
    printf("%lld\n",ans);
}
int main(){
    int t=1;
    //scanf("%d",&t);
    for(int T=1;T<=t;T++){
        solve(T);
    }
}
/*
3 3
oxo
xxo
ooo
4 3
oxx
xxo
xxo
ooo
3 4
xooo
xxxo
oxxo
4 3
ooo
oxx
oxx
xxo
3 4
oxxo
oxxx
ooox
*/
accuracy: 0.03278688524590164
time_ms: 300
memory_kb: 6928
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)

#define tct template<class t>
#define tctu template<class t,class u>

tctu bool chmax(t&a,u b){
	if(a<b){
		a=b;
		return true;
	}else return false;
}
tctu bool chmin(t&a,u b){
	if(a>b){
		a=b;
		return true;
	}else return false;
}

tct using vc=vector<t>;
tct using vvc=vc<vc<t>>;
using vi=vc<int>;

#define pb push_back
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())

tct void mkuni(vc<t>&vs){
	sort(all(vs));
	vs.erase(unique(all(vs)),vs.ed);
}

const int inf=INT_MAX;

struct N{
	int m,l;
	vi x,y;
	void init(){
		cin>>m>>l;
		x.resize(m);
		y.resize(m);
		rep(i,m){
			cin>>x[i]>>y[i];
		}
	}
	int ask(int t){
		rep(i,m){
			if(t==x[i])return y[i];
			if(i+1<m&&x[i]<=t&&t<=x[i+1]){
				return y[i]+(y[i+1]-y[i])/(x[i+1]-x[i])*(t-x[i]);
			}
		}
		return inf;
	}
};

void slv(){
	int n;cin>>n;
	vc<N> a(n);
	rep(i,n)a[i].init();
	vi pos;
	rep(i,n)for(auto p:a[i].x)pos.pb(p);
	mkuni(pos);
	int m=si(pos);
	
	vi len(1<<n);
	rep(bit,1<<n){
		rep(i,n)if(bit&1<<i)len[bit]+=a[i].l;
	}
	
	vvc<int> rt(m,vi(1<<n,inf));
	vvc<int> lf(m,vi(1<<n,inf));
	
	rep(i,m){
		rt[i][0]=0;
		rep(bit,1<<n)if(rt[i][bit]<inf){
			rep(j,n)if((bit&1<<j)==0){
				int w=a[j].ask(pos[i]+len[bit]);
				if(w<inf){
					chmin(rt[i][bit|1<<j],rt[i][bit]+w);
				}
			}
		}
		lf[i][0]=0;
		rep(bit,1<<n)if(lf[i][bit]<inf){
			rep(j,n)if((bit&1<<j)==0){
				int w=a[j].ask(pos[i]-len[bit]-a[j].l);
				if(w<inf){
					chmin(lf[i][bit|1<<j],lf[i][bit]+w);
				}
			}
		}
	}
	
	auto dp=lf;
	vi buf(1<<n);
	rep(i,m)rng(j,i+1,m){
		fill(all(buf),inf);
		rep(bit,1<<n)if(rt[i][bit]<inf){
			int rem=(1<<n)-1-bit,cur=rem;
			do{
				if(lf[j][cur]<inf){
					chmin(buf[bit|cur],rt[i][bit]+lf[j][cur]);
				}
				cur=(cur-1)&rem;
			}while(rem!=cur);
		}
		rep(bit,1<<n)if(len[bit]>pos[j]-pos[i])buf[bit]=inf;
		rep(bit,1<<n)if(dp[i][bit]<inf){
			int rem=(1<<n)-1-bit,cur=rem;
			do{
				if(buf[cur]<inf){
					chmin(dp[j][bit|cur],dp[i][bit]+buf[cur]);
				}
				cur=(cur-1)&rem;
			}while(rem!=cur);
		}
	}
	
	int ans=inf;
	rep(i,m){
		rep(bit,1<<n)if(dp[i][bit]<inf&&rt[i][(1<<n)-1-bit]<inf)
			chmin(ans,dp[i][bit]+rt[i][(1<<n)-1-bit]);
	}
	cout<<ans<<endl;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	
	slv();
}
accuracy: 1
time_ms: 250
memory_kb: 4660
final_rank: 1