OK = false ; while(!OK) { in(); int t = k; while(a[t] == n + t - k) t--; if(t>0) { a[t]++; for(int i = t+1; i<= k ; i++) { a[i] = a[i-1] +1; } } else OK = true; }