http://www.pipioj.online/problem.php?id=1208
#define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define bug(x) cout<<#x<<" is "<<x<<endl #include <bits/stdc++.h> #define iter ::iterator using namespace std; typedef long long ll; typedef pair<int,ll>P; #define pb push_back #define mk make_pair #define se second #define fi first #define rs o*2+1 #define ls o*2 const ll mod=1e9+7; const int N=1e2+5; int T,n,k; char s[N]; int a[N]; int main(){ //IO; while(~scanf("%d%d",&n,&k)){ scanf("%s",s+1); for(int i=1;i<=n;i++)a[i]=s[i]-'a'+1; sort(a+1,a+1+n); int ans=a[1],pre=a[1],cnt=1; for(int i=2;i<=n;i++){ if(cnt==k)break; if(a[i]>=pre+2){ ans+=a[i]; cnt++; pre=a[i]; } } if(cnt!=k)ans=-1; printf("%d ",ans); } }