#include<stdio.h> #include<stdlib.h> int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main(){ int m, n; long long masa[26], plecak[105]; scanf("%d",&n); scanf("%d", &m); int ile=m; for(int i=0;i<n;i++) scanf("%d", &masa[i]); for(int i=0;i<m;i++) scanf("%d", &plecak[i]); qsort (plecak, m, sizeof(long long), compare); int ile_p=1; int ile_rz=0; int in; long long p=plecak[m-1]; int j=0; while((j<n)&&(ile_rz!=n)&&(ile_p!=ile)){ long long max=masa[0]; j++; int f=0; for(int i=0;i<n;i++){ if((max<=masa[i])&&(masa[i]<=p)&&(masa[i]!=0)) { max=masa[i]; in=i; i++; } if(max==0){ m--; p=plecak[m-1]; ile_p++; if(ile_p>ile){ printf("NIE"); return 0; } } else { masa[in]=0; p=p-max; ile_rz++; } }} if(ile_rz==n) printf("%d", ile_p); else printf("NIE\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include<stdio.h> #include<stdlib.h> int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main(){ int m, n; long long masa[26], plecak[105]; scanf("%d",&n); scanf("%d", &m); int ile=m; for(int i=0;i<n;i++) scanf("%d", &masa[i]); for(int i=0;i<m;i++) scanf("%d", &plecak[i]); qsort (plecak, m, sizeof(long long), compare); int ile_p=1; int ile_rz=0; int in; long long p=plecak[m-1]; int j=0; while((j<n)&&(ile_rz!=n)&&(ile_p!=ile)){ long long max=masa[0]; j++; int f=0; for(int i=0;i<n;i++){ if((max<=masa[i])&&(masa[i]<=p)&&(masa[i]!=0)) { max=masa[i]; in=i; i++; } if(max==0){ m--; p=plecak[m-1]; ile_p++; if(ile_p>ile){ printf("NIE"); return 0; } } else { masa[in]=0; p=p-max; ile_rz++; } }} if(ile_rz==n) printf("%d", ile_p); else printf("NIE\n"); return 0; } |