let suppose array stack s initially array b initially emptyyou perform follow operation s take element push s remove take element s append end array b remove s s you perform operation arbitrary orderif exist way perform operation array b sorted nondescending order end array call stacksortablefor example 312 stacksortable b sorted perform follow operation remove 3 push s remove 1 push s remove 1 s append end b remove 2 push s remove 2 s append end b remove 3 s append end b after operation b123 312 stacksortable 231 stacksortableyou give k element permutation p size n recall permutation size n array size n integer 1 n occur exactly you restore remain nk element permutation stacksortable if multiple answer choose answer p lexicographically maximal array q lexicographically great array p iff exist integer k ik qipi qkpk you swap change k element permutationprint lexicographically maximal permutation p obtainIf exist answer output 1