Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<stdio.

h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int c[100][100];
int p[10000];
int elm[100];
int pos[100]={0};
int n,m,i,j,sum;
sum=0;
cin>>n>>m;
for(i=0;i<m;i++)
cin>>p[i
];
for(j=0;j<n;j++){
cin>>elm
[j];
for(i=0;
i<elm[j];i++){
cin>>c[j][i];
}
}
for(i=0;i<n;i++){
std::sor
t(c[i],c[i]+(elm[i]));
std::reverse(c[i],c[i]+(elm[i]));
}
for(i=0;i<m;i++){
if(elm[p
[i]]>0){
sum=sum+c[p[i]][pos[p[i]]];
pos[p[i]]++;
elm[p[i]]--;
}
}
cout<<sum<<"\n";
}
return 0;
}

You might also like