Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- static Scanner s = new Scanner(System.in);
- static int len(int k){
- int c = 0;
- while (k > 0){
- c++;
- k /= 10;
- }
- return c;
- }
- static int gcd(int a, int b){
- if (a > b) {
- int t = a + b;
- a = t - a;
- b = t - b;
- }
- if (a == 0) return b;
- if (a == 1) return 1;
- return gcd(a, b % a);
- }
- static void solve(){
- int n = s.nextInt();
- }
- static class ModSet{
- long [] a;
- TreeSet<Pair> set;
- TreeSet<Integer> active = new TreeSet<>();
- ModSet(int n){
- a = new long [n+1];
- set = new TreeSet<>();
- }
- void add(long x, int y){
- a[y] = x;
- active.add(y);
- set.add(new Pair(x, y));
- }
- void update(int y, long k){
- Pair d = new Pair(a[y], y);
- set.remove(d);
- a[y] = k;
- d.first = k;
- set.add(d);
- }
- Pair first(){
- Pair p = set.first();
- set.remove(p);
- active.remove(p.second);
- return p;
- }
- int ceeling(int x){
- return active.higher(x);
- }
- long num(int x){
- return a[x];
- }
- }
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int tc = s.nextInt();
- for (int bgb = 0; bgb < tc; bgb++) {
- int n = s.nextInt();
- int [] a = new int [n+1];
- for (int i = 0; i < n; i++) {
- a[i+1] = s.nextInt();
- }
- int [] pref = prefsumma(a);
- int [] v = new int [n+1];
- for (int i = n-1; i > -1; i--) {
- v[i] = v[i+1] + a[i+1];
- }
- ModSet set = new ModSet(n);
- int [] interested = new int [n+1];
- int [] used = new int [n+1];
- long sum = 0;
- long k;
- for (int i = 1; i <= n; i++) {
- interested[i] = 1;
- set.add(a[i] - v[i], i);
- sum += (i - 1) * a[i];
- }
- k = sum;
- HashSet<Integer> ans = new HashSet<>();
- int last = 0;
- for (int t = n - 1; t > 0; t--) {
- //Postavil continue --;
- Pair p = set.first();
- for (int i = 0; i <= n; i++) {
- System.out.println(i + " " + set.a[i]);
- }
- if (used[p.second] == 1){
- }
- if (t == 1){
- last = p.second;
- }
- used[p.second] = 1;
- System.out.println(p.first + " " + p.second);
- int next = set.ceeling(p.second);
- long k1 = set.num(next);
- k1 += pref[p.second] - pref[p.second-interested[p.second]];
- interested[next] += interested[p.second];
- set.update(next, k1);
- k += p.first;
- if (sum > k){
- sum = k;
- for (int m :
- set.active) {
- ans.add(m);
- }
- }
- }
- if (set.a[last] < 0){
- sum += -a[last];
- }
- System.out.println(sum);
- for (int m:
- ans) {
- System.out.print(m + " ");
- }
- System.out.println();
- }
- }
- static class Pair implements Comparable<Pair>{
- long first;
- int second;
- public Pair(long first, int second) {
- this.first = first;
- this.second = second;
- }
- @Override
- public int compareTo(Pair pair) {
- if (this.first == pair.first) return second - pair.second; else return Long.compare(first, pair.first);
- }
- }
- static int [] prefsumma(int [] a){
- int n = a.length - 1;
- int [] pref = new int [n + 1];
- for (int i = 1; i <= n; i++) {
- pref[i] = pref[i-1] + a[i];
- }
- return pref;
- }
- }
Add Comment
Please, Sign In to add comment