------
## 最终结论
```python
def lexicographically_smallest_string(S: str, K: int) -> str:
    if K == 1:
        # When K is 1, we can only rotate the string
        smallest = S
        for i in range(len(S)):
            rotated = S[i:] + S[:i]
            if rotated < smallest:
                smallest = rotated
        return smallest
    else:
        # When K > 1, we can sort the entire string
        return ''.join(sorted(S))

# Example usage:
print(lexicographically_smallest_string("cba", 1))  # Output: "acb"
print(lexicographically_smallest_string("baaca", 3))  # Output: "aaabc"
```