package b0;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/* JADX INFO: loaded from: classes.dex */
public final class y {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static final Comparator f3098h = new a();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static final Comparator f3099i = new b();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final int f3100a;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f3104e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private int f3105f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f3106g;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final c[] f3102c = new c[5];

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final ArrayList f3101b = new ArrayList();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private int f3103d = -1;

    class a implements Comparator {
        a() {
        }

        @Override // java.util.Comparator
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(c cVar, c cVar2) {
            return cVar.f3107a - cVar2.f3107a;
        }
    }

    class b implements Comparator {
        b() {
        }

        @Override // java.util.Comparator
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(c cVar, c cVar2) {
            float f2 = cVar.f3109c;
            float f3 = cVar2.f3109c;
            if (f2 < f3) {
                return -1;
            }
            return f3 < f2 ? 1 : 0;
        }
    }

    private static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public int f3107a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public int f3108b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public float f3109c;

        private c() {
        }
    }

    public y(int i2) {
        this.f3100a = i2;
    }

    private void b() {
        if (this.f3103d != 1) {
            Collections.sort(this.f3101b, f3098h);
            this.f3103d = 1;
        }
    }

    private void c() {
        if (this.f3103d != 0) {
            Collections.sort(this.f3101b, f3099i);
            this.f3103d = 0;
        }
    }

    public void a(int i2, float f2) {
        c cVar;
        b();
        int i3 = this.f3106g;
        if (i3 > 0) {
            c[] cVarArr = this.f3102c;
            int i4 = i3 - 1;
            this.f3106g = i4;
            cVar = cVarArr[i4];
        } else {
            cVar = new c();
        }
        int i5 = this.f3104e;
        this.f3104e = i5 + 1;
        cVar.f3107a = i5;
        cVar.f3108b = i2;
        cVar.f3109c = f2;
        this.f3101b.add(cVar);
        this.f3105f += i2;
        while (true) {
            int i6 = this.f3105f;
            int i7 = this.f3100a;
            if (i6 <= i7) {
                return;
            }
            int i8 = i6 - i7;
            c cVar2 = (c) this.f3101b.get(0);
            int i9 = cVar2.f3108b;
            if (i9 <= i8) {
                this.f3105f -= i9;
                this.f3101b.remove(0);
                int i10 = this.f3106g;
                if (i10 < 5) {
                    c[] cVarArr2 = this.f3102c;
                    this.f3106g = i10 + 1;
                    cVarArr2[i10] = cVar2;
                }
            } else {
                cVar2.f3108b = i9 - i8;
                this.f3105f -= i8;
            }
        }
    }

    public float d(float f2) {
        c();
        float f3 = f2 * this.f3105f;
        int i2 = 0;
        for (int i3 = 0; i3 < this.f3101b.size(); i3++) {
            c cVar = (c) this.f3101b.get(i3);
            i2 += cVar.f3108b;
            if (i2 >= f3) {
                return cVar.f3109c;
            }
        }
        if (this.f3101b.isEmpty()) {
            return Float.NaN;
        }
        return ((c) this.f3101b.get(r5.size() - 1)).f3109c;
    }
}
