样例输入1
3 4 2 9 6 2 3 7 1 5 3 2 2 3 3 1 1 1
17
易卡测试点
20 20
308 481 509 429 701 328 527 40 899 163 929 602 776 771 897 571 406 907 292 327
9 22 398 136 175 142 17 148 404 182
4 267 20 178 189
11 139 167 296 145 25 23 498 218 330 358 322
13 260 438 9 126 262 334 439 55 115 275 408 136 105
11 246 95 278 54 114 261 473 247 404 208 8
10 308 42 68 195 114 105 198 167 410 460
11 169 433 58 368 125 158 127 276 331 355 370
2 415 141
13 67 299 465 94 436 334 224 54 242 450 63 33 13
5 30 356 1 143 215
18 403 237 101 419 476 38 276 48 247 259 125 211 227 250 97 436 57 414
14 110 36 336 95 142 134 421 181 445 363 110 336 326 248
18 417 500 198 301 263 449 248 477 67 173 2 429 87 23 420 222 39 431
9 12 450 206 418 23 86 202 497 289
1 234
14 405 65 188 205 480 112 325 400 421 242 66 207 495 75
14 177 272 376 121 298 396 118 425 313 447 94 474 236 479
14 114 306 249 198 230 11 397 289 110 338 150 385 75 279
7 149 102 209 440 132 92 143
10 492 109 371 436 103 93 169 54 66 263
9027
#include <iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
using LL =long long;
const int N=1010;
vector<int>a;
priority_queue<int>b[N];
int c[N];
int n,m;
int flag;
LL sum;
int max_t;
int main()
{
cin>>m>>n;
for(int i=1;i<=m;i++){
int x;cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
for(int i=1;i<=n;i++){
int k;cin>>k;
c[i]=k;
if(k>m)flag=-1;
for(int j=1;j<=k;j++){
int x;cin>>x;
b[i].push(x);
max_t=max(max_t,x);
}
}
if(flag==-1){
cout<<"-1";
return 0;
}
while(a.size()){
int t=lower_bound(a.begin(),a.end(),max_t)-a.begin();
// cout<<max_t<<"--"<<t<<"---"<<a[t]<<"\n";
if(a.begin()+t==a.end()){flag=-1;break;}
int tt=a[t];
a.erase(a.begin()+t);
max_t=0;
int count=0;
for(int i=1;i<=n;i++){
if(b[i].empty())continue;
int p=b[i].top();
if(tt>=p){
b[i].pop();
count++;
if(b[i].size())max_t=max(max_t,b[i].top());
}
}
if(count)sum+=tt;
}
for(int i=1;i<=n;i++){
if(b[i].empty())continue;
flag=-1;
}
if(flag==-1)cout<<"-1";
else cout<<sum;
return 0;
}